【问题标题】:Drupal 7 TypeError for jQuery script: $ is not a functionjQuery 脚本的 Drupal 7 TypeError:$ 不是函数
【发布时间】:2014-05-18 10:26:52
【问题描述】:

我有一些 JS 代码,但 Drupal 7 无法识别它。我收到以下错误:

TypeError: $ is not a function

谁能帮我制作这个脚本?我正在使用 jQuery v1.4.4。

<script type="text/javascript">
this.screenshotPreview = function(){    
/* CONFIG */

    xOffset = 10;
    yOffset = 30;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

/* END CONFIG */
$("a.screenshot").hover(function(e){
    this.t = this.title;
    // this.title = "";    
    var c = (this.t != "") ? "<br/>" + this.t : "";
    $("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");                                
    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px")
        .fadeIn("fast");                        
},
function(){
    this.title = this.t;    
    $("#screenshot").remove();
}); 
$("a.screenshot").mousemove(function(e){
    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px");
});         
};


// starting the script on page load
$(document).ready(function(){
screenshotPreview('some text');
});
</script>

【问题讨论】:

  • 你必须在你的脚本之前包含 jQuery。
  • jQuery 包含在头部脚本的顶部
  • 对于应该处理元素的代码,您可能需要使用 Drupal.behaviors。有关一些解释,请参阅pbuyle.github.io/blog/2013/11/18/…。

标签: javascript jquery drupal drupal-7


【解决方案1】:

尝试将“$”快捷方式的所有实例更改为“jQuery”,它应该可以工作。例如,调用 screenshotPreview 函数将如下所示:

// starting the script on page load
jQuery(document).ready(function(){
screenshotPreview('some text');
});

或者将所有 jQuery 代码包含在一个以 jQuery 作为参数的函数中,然后 $ 快捷方式应该可以工作。

// We define a function that takes one parameter named $.
(function ($) {
  // Use jQuery with the shortcut:
  console.log($.browser);
// Here we immediately call the function with jQuery as the parameter.
}(jQuery));

(来源:https://drupal.org/node/171213)

【讨论】:

【解决方案2】:

Drupal 7 在no-conflict mode 中提供了jQuery,这意味着$ 不是jQuery 对象/命名空间。对于遵循 jQuery's plugins authoring documentation 的正确编写的 jQuery 插件,这应该不是问题。

期望 $ 成为 jQuery 命名空间的 JavaScript 代码将无法在 Drupal 页面中运行。这可以通过将代码包装在一个立即调用的匿名函数中轻松解决,该函数将 jQuery 命名空间别名为$:

(function($) {
    // Here $ is the jQuery namespace.
})(jQuery);

【讨论】:

    【解决方案3】:

    试试这个:(如果不对,请告诉我 - 但它似乎对我有用)

    /*******************************************************************
    *                    :: Define Your Functions ::
    *******************************************************************/ 
    
    (function ($) {
        removeAjaxLoader = function() {
            console.log('removeAjaxLoader');
            $('body').find('.ajax-loader').remove();
            $('body').find('.ajax-loader-icon').remove();
            return false;
        } //removeAjaxLoader
    
        addAjaxLoader = function() {
            console.log('addAjaxLoader');
            removeAjaxLoader();
            $('body').append('<div class="ajax-loader"></div><div class="ajax-loader-icon"></div>');
            return false;
        } //addAjaxLoader
    }) (jQuery);
    
    /*******************************************************************
    *                        :: Ready Set Go ::
    *******************************************************************/ 
    (function ($) {
        $(document).ready(function() {
            console.log('ready complete');
            removeAjaxLoader();
    
        }); // document.ready
    }) (jQuery);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 2015-05-09
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多