javascript “类型错误:$(...) 为空”这是怎么回事?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16171681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"TypeError: $(...) is null" What's Going On?
提问by Joshua
Firebug is giving the error:
Firebug 给出了错误:
TypeError: $(...) is null
$('#menu img').hover(
I have no idea why. The seemingly problematic script is this which replaces an image when the cursor hovers over an image:
我不知道为什么。看似有问题的脚本是当光标悬停在图像上时替换图像:
$(document).ready(function()
{
var storeNormalPath;
$('#menu img').hover(
function()
{
storeNormalPath = $(this).attr('src');
var imgArray = $(this).attr('src').split('.jpg');
$(this).attr('src', imgArray[0]+'Hover.jpg');
},
function()
{
$(this).attr('src', storeNormalPath);
}
).click (function()
{
//empty now
});
});
回答by dayala
After some looking over your page using chrome's console, it looks that even $(document)
is returning null.
jQuery(document)
however, works, which suggests there is something in conflict with jQuery's $ operator.
在使用 chrome 的控制台查看您的页面后,它看起来甚至$(document)
返回 null。
jQuery(document)
但是,有效,这表明与 jQuery 的 $ 运算符存在冲突。
(Source: I would direct you to this question: $.document is null)
(来源:我会引导你回答这个问题:$.document is null)
Seeing that you have both jquery-1.5.1.min.jsAND jquery.1.4.2.jsreferenced in your page header, perhaps that could be the reason of the conflict? Have you tried loading only one of them?
看到您的页眉中同时引用了jquery-1.5.1.min.js和jquery.1.4.2.js,也许这可能是冲突的原因?你试过只加载其中之一吗?
Let us know if that helps, sorry I couldn't be more help. Good luck!
如果这有帮助,请告诉我们,对不起,我无法提供更多帮助。祝你好运!