WordPress外部脚本不起作用 [英] Wordpress external script not working

查看:166
本文介绍了WordPress外部脚本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此功能来加载jQuery和自定义脚本:

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();

    ...
}

这些div仅位于wordpress的一个站点上. jQuery已成功加载,但div并未隐藏.有任何想法吗?非常感谢.

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.

推荐答案

首先,您不应该注销jQuery或加载它,而应该添加脚本的依赖项,而WordPress会处理其余部分

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' );
}

其次,jQuery在Wordpress中以noConflict模式运行

secondly, jQuery runs in noConflict mode in Wordpress

jQuery(document).ready(function($) {

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

});

这篇关于WordPress外部脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆