jQuery Uncaught TypeError:对象[对象窗口]的属性'$'不是函数

时间:2012-05-29 21:50:46

标签: jquery

全部, 我下载了一个预先绑定的JS / CSS表单应用程序,我试图在Wordpress中使用它。我有以下代码:

$(document).ready(function () {


/*----------------------------------------------------------------------*/
/* Parse the data from an data-attribute of DOM Elements
/*----------------------------------------------------------------------*/


$.parseData = function (data, returnArray) {
    if (/^\[(.*)\]$/.test(data)) { //array
        data = data.substr(1, data.length - 2).split(',');
    }
    if (returnArray && !$.isArray(data) && data != null) {
        data = Array(data);
    }
    return data;
};

/*----------------------------------------------------------------------*/
/* Image Preloader
/* http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
/*----------------------------------------------------------------------*/



// Arguments are image paths relative to the current page.
$.preload = function() {
    var cache = [],
        args_len = arguments.length;
    for (var i = args_len; i--;) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        cache.push(cacheImage);
    }
};


/*----------------------------------------------------------------------*/
/* fadeInSlide by revaxarts.com
/* Fades out a box and slide it up before it will get removed
/*----------------------------------------------------------------------*/


$.fn.fadeInSlide = function (speed, callback) {
    if ($.isFunction(speed)) callback = speed;
    if (!speed) speed = 200;
    if (!callback) callback = function () {};
    this.each(function () {

        var $this = $(this);
        $this.fadeTo(speed / 2, 1).slideDown(speed / 2, function () {
            callback();
        });
    });
    return this;
};


/*----------------------------------------------------------------------*/
/* fadeOutSlide by revaxarts.com
/* Fades out a box and slide it up before it will get removed
/*----------------------------------------------------------------------*/


$.fn.fadeOutSlide = function (speed, callback) {
    if ($.isFunction(speed)) callback = speed;
    if (!speed) speed = 200;
    if (!callback) callback = function () {};
    this.each(function () {

        var $this = $(this);
        $this.fadeTo(speed / 2, 0).slideUp(speed / 2, function () {
            $this.remove();
            callback();
        });
    });
    return this;
};

/*----------------------------------------------------------------------*/
/* textFadeOut by revaxarts.com
/* Fades out a box and slide it up before it will get removed
/*----------------------------------------------------------------------*/


$.fn.textFadeOut = function (text, delay, callback) {
    if (!text) return false;
    if ($.isFunction(delay)) callback = delay;
    if (!delay) delay = 2000;
    if (!callback) callback = function () {};
    this.each(function () {

        var $this = $(this);
        $this.stop().text(text).show().delay(delay).fadeOut(1000,function(){
            $this.text('').show();
            callback();
        })
    });
    return this;
};

/*----------------------------------------------------------------------*/
/* leadingZero by revaxarts.com
/* adds a leding zero if necessary
/*----------------------------------------------------------------------*/


$.leadingZero = function (value) {
    value = parseInt(value, 10);
    if(!isNaN(value)) {
        (value < 10) ? value = '0' + value : value;
    }
    return value;
};


});

我假设Wordpress没有冲突导致问题所以我更新了最后一个括号,如下所示:

}, "jQuery");

但是,我仍然遇到同样的错误。有谁知道会出现什么问题以及如何解决这个问题?

提前致谢!

5 个答案:

答案 0 :(得分:261)

这是一个语法问题,WordPress附带的jQuery库以“无冲突”模式加载。这是为了防止WordPress可以加载的其他JavaScript库的兼容性问题。在“无争议”模式下,$快捷方式不可用,并且使用了更长的jQuery,即

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

通过在函数调用后包括$ in括号,您可以在代码块中使用此快捷方式。

有关详细信息,请参阅WordPress Codex

答案 1 :(得分:34)

我最喜欢的不冲突友好的构造:

jQuery(function($) {
  // ...
});

使用函数指针调用jQuery是$(document).ready(...)

的快捷方式

或者正如我们在coffeescript中所说:

jQuery ($) ->
  # code here

答案 2 :(得分:6)

在Wordpress中只需替换

$(function(){...});

jQuery(function(){...});

答案 3 :(得分:1)

您可以考虑将默认的WordPress jQuery脚本替换为Google Library,方法是在theme functions.php文件中添加以下内容:

function modify_jquery() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2');
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'modify_jquery');

从此处获取的代码:http://www.wpbeginner.com/wp-themes/replace-default-wordpress-jquery-script-with-google-library/

答案 4 :(得分:-1)

也许你在jquery之前有这样的代码:

var $jq=jQuery.noConflict();
$jq('ul.menu').lavaLamp({
    fx: "backout", 
    speed: 700
});

他们是冲突

您可以将$更改为(jQuery)

相关问题