duanpin2009 2016-09-24 23:57
浏览 54
已采纳

Wordpress外部脚本无法正常工作

I am using this function in order to load jQuery and my custom script:

function.php

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');

    // Register and Enqueue a Script
    // get_stylesheet_directory_uri will look up child theme location
    wp_register_script( 'FormScript', get_stylesheet_directory_uri() . '/FormScript.js');
    wp_enqueue_script( 'FormScript' );
}

This is a snippet of my custom script:

if (typeof jQuery == 'undefined') {  
    // jQuery is not loaded
    alert("false");
} else {
    // jQuery is loaded
    alert("true");
}

$(document).ready(function() {

    $("#Main").hide();
    $("#Angehoerigkeit").hide();
    $("#Alter").hide();
    $("#Image").hide();

    ...
}

These div's are only located on one site in wordpress. The jQuery is loaded successfully but the div's aren't hiding. Any ideas? Thanks a lot.

  • 写回答

1条回答 默认 最新

  • douchujian8124 2016-09-25 00:04
    关注

    Firstly, you shouldn't be deregistering jQuery, or loading it, you should just add is a dependency for your script, and Wordpress will take care of the rest

    if (!is_admin()) {
        add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
    }
    
    function my_jquery_enqueue() {
        wp_register_script( 'FormScript', get_stylesheet_directory_uri() . '/FormScript.js', array('jquery'));
        wp_enqueue_script( 'FormScript' );
    }
    

    secondly, jQuery runs in noConflict mode in Wordpress

    jQuery(document).ready(function($) {
    
        $("#Main").hide();
        $("#Angehoerigkeit").hide();
        $("#Alter").hide();
        $("#Image").hide();
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值
  • ¥20 数据排序,可选择排序方向
  • ¥15 修改一下代码,考虑进程到达时间不同的情况
  • ¥15 帮我看看这是个啥题,带解题过程和结果,条件如下FCF = 290471.33 g1 = 15% r = 8% g2 = 4% n = 5