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 网站出现错误跳转问题
  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝
  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403