【问题标题】:jQuery Uncaught TypeError: Property '$' of object [object Window] is not a functionjQuery Uncaught TypeError:对象[对象窗口]的属性'$'不是函数
【发布时间】:2023-03-23 03:08:01
【问题描述】:

所有, 我下载了一个预先捆绑的 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");

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

提前致谢!

【问题讨论】:

    标签: jquery


    【解决方案1】:

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

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

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

    详情请见WordPress Codex

    【讨论】:

    • 我应该补充一下,你需要在最后松开“jQuery”
    • 你真是个救命恩人!!!经过 3 天的调试,我发现这正是我的问题的解决方案。虽然 WordPress 正在加载 jQuery 文件,但我无法访问准备好的文档中提到的功能。因此,这行确切的代码,即jQuery(document).ready(function ($) { 将其永久修复。非常感谢分享。
    • 这也是 Drupal 中的相同问题。解决方案也在那里工作。谢谢
    【解决方案2】:

    我最喜欢的无冲突友好结构:

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

    使用函数指针调用 jQuery 是 $(document).ready(...) 的快捷方式

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

    jQuery ($) ->
      # code here
    

    【讨论】:

    • 如果 $ 已经是一个 jquery 实例 - 有什么理由传递 jQuery 并再次给它 $ 名称?
    • $ 可能不是 jQuery 实例,如果它与另一个库冲突 - 无冲突模式。
    • 这是$(document).ready()的快捷方式,而不是$(document).on('load')
    • 这对我有用!我主题中的所有 custom.js 函数都莫名其妙地坏了。我用这个替换了 $(document).ready(function(){ ,它就像魔术一样工作:)
    【解决方案3】:

    在 Wordpress 中只需替换

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

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

    【讨论】:

      【解决方案4】:

      您可以考虑将默认的 WordPress jQuery 脚本替换为 Google Library,方法是在主题 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/

      【讨论】:

      • 如果您在 WP 之外使用“外部”JQuery 插件的最佳解决方案 缺点: - 可能 - 与一些 Wordpress 插件冲突 - 我这边没有注意到
      【解决方案5】:

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

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

      他们是冲突

      你可以把 $ 改成 (jQuery)

      【讨论】:

      • 我像 var $=jQuery.noConflict(); 一样使用它,我的是一个 java-webapp 设置,但我得到了同样的错误!
      猜你喜欢
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 2014-09-07
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多