pro*_*yse 2 javascript jquery drupal drupal-7
我有一些JS代码,但Drupal 7无法识别它.我收到以下错误:
TypeError: $ is not a function
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我使这个脚本工作吗?我正在使用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>
Run Code Online (Sandbox Code Playgroud)
尝试将"$"快捷方式的所有实例更改为"jQuery",它应该可以正常工作.例如,调用screenshotPreview函数将如下所示:
// starting the script on page load
jQuery(document).ready(function(){
screenshotPreview('some text');
});
Run Code Online (Sandbox Code Playgroud)
或者将所有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));
Run Code Online (Sandbox Code Playgroud)
(来源:https://drupal.org/node/171213)
归档时间: |
|
查看次数: |
11766 次 |
最近记录: |