WordPress外部脚本无法正常工作

我正在使用此函数来加载jQuery和我的自定义脚本:

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

这是我的自定义脚本的片段:

 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没有隐藏。 有任何想法吗? 非常感谢。

首先,你不应该取消注册jQuery,或加载它,你应该只添加是你的脚本的依赖项,Wordpress将负责其余的

 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模式运行

 jQuery(document).ready(function($) { $("#Main").hide(); $("#Angehoerigkeit").hide(); $("#Alter").hide(); $("#Image").hide(); });