【发布时间】: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