jQuery没有使用WordPress

时间:2011-10-03 15:41:23

标签: javascript jquery wordpress

我认为与WordPress使用jQuery的方式存在冲突:

尝试:

(function($) {
    $(document).ready(function(){
        alert('hello');
    });
}(jQuery));

也试过:

$(document).ready(function(){
    alert('hello');
});

Firebug转储:

jQuery is not defined
[Detener en este error] }(jQuery)); 

还尝试过:

jQuery.noConflict();
jQuery(document).ready(function($) {
   alert("hello");
});

和Firebug:

jQuery is not defined
[Detener en este error] jQuery.noConflict(); 

jQuery 导入

知道我错过了什么吗?

5 个答案:

答案 0 :(得分:3)

看起来jQuery不可用($变量和jQuery都没有定义)。在您的特定情况下,这是因为库根本没有加载。将下一个代码放在<head>部分之前您需要jQuery的脚本中:

<script src="//code.jquery.com/jquery-1.6.4.min.js"></script>

答案 1 :(得分:2)

你是如何包括jquery的?无论情况如何,它都没有发生(查看头部/通过firebug检查页面DOM - 没有$或jQuery引用)。

在wordpress中链接托管版本的jquery(在本例中为依赖插件)的正确方法如下所示:

functions.php (或插件......或其他)

// register scripts 
if (! function_exists(thickbox_register){
function thickbox_register() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery','http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
    wp_register_script( 'thickbox', 'path to thickbox'.thickbox.js, 'jquery');
    }  
}   
add_action('init', 'thickbox_register');

//print the now registered scripts 
if (! function_exists(thickbox_enqueue){
function thickbox_enqueue() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'thickbox' );
    }  
}
add_action('wp_print_scripts', 'thickbox_enqueue');

// do the same for css 
if (! function_exists(thickbox_style_enqueue){
function thickbox_style_enqueue() {
    wp_register_style( 'thickbox', 'path to css'.thickbox.css );
    wp_enqueue_style( 'thickbox' );
    }  
}
add_action('wp_print_styles', 'thickbox_style_enqueue');

要确保wordpress在正确的时间提供正确的脚本,您必须向init添加操作并注册脚本wordpress方法。

希望有所帮助

答案 2 :(得分:2)

即使您的文档现在有效,也可以获得更好的效果:

  1. 使用“Jquery”更改所有$符号。 (与wordpress核心冲突)
  2. 你没有宣布课程或身份证明。
  3. 例如在您的代码中:

      $('header').hide();
    

    应该是

    jQuery('#branding').hide();
    

    $('body').css('position','relative');
    

    应该是

    jQuery('.home').css('position','relative');
    

    你的代码是语义的,其中header不是标签,而是标签,意味着header不是类或id(而是#branding是id)和body(而不是.home或.blog) 更好的做法是宣布上课。

答案 3 :(得分:0)

检查了您发布的网址,似乎没有对jQuery的引用。

在头部

中包含以下代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"> </script>

答案 4 :(得分:0)

尝试将此代码放入<head>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>