繁体   English   中英

用于jQuery脚本的Drupal 7 TypeError:$不是函数

[英]Drupal 7 TypeError for jQuery script: $ is not a function

我有一些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”,它应该可以正常工作。 例如,调用screenshotPreview函数将如下所示:

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

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

// 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

Drupal 7在无冲突模式下提供jQuery,这意味着$不是jQuery对象/命名空间。 对于遵循jQuery插件创作文档的正确编写的jQuery插件,这不应该是一个问题。

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

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

试试这个:(或者让我知道,如果这不对 - 但它似乎对我有用)

/*******************************************************************
*                    :: 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM