【发布时间】:2012-03-03 02:05:25
【问题描述】:
这是我在网上找到的一个tooltip js函数,由于防御性编程,重新编码。但是原始代码和我的代码具有相同的 Google Chrome 控制台错误消息。
消息说:
未捕获的引用错误:$ 未定义。
正在为以下行生成错误。
$(document).ready(function(){...
代码运行良好,没有错误。在主函数中,控制台没有给出$ 标志的错误消息。
一个例子:
$(document).ready(function(){... //no error on console for "$"
那么,这是 Chrome 浏览器控制台的错误还是我的错?
this.tooltip = function(){
/* CONFIG */
xOffset = 10;
yOffset = 20;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */
$("a.tooltip").hover(function(e){
if (this.t)
this.t = this.title;
this.title = "";
$("body").append("<p id='tooltip'>"+ this.t +"</p>");
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
},
function(){
this.title = this.t;
$("#tooltip").remove();
});
$(".tooltip").mousemove(function(e){
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
};
$(document).ready(function(){
tooltip();
});
【问题讨论】:
-
那么,您在控制台中看到了
$ is not defined,但代码工作正常吗? -
是的... main.js 中的这段代码。我从我的 html 中调用它; 跨度>
-
Inception 更容易理解......你不能只说“工具提示功能”或“准备好的处理程序”吗?
-
首先包含 jQuery,然后是 main.js 脚本!
-
$ 在 Chrome 控制台中起作用的原因是因为它 maps to
document.querySelector为开发人员提供方便...以前是 document.getElementById。等等,这个问题太老了……
标签: javascript jquery html