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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:33:47  来源:igfitidea点击:

"TypeError: $(...) is null" What's Going On?

javascriptfirebugtypeerror

提问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.jsjquery.1.4.2.js,也许这可能是冲突的原因?你试过只加载其中之一吗?

Let us know if that helps, sorry I couldn't be more help. Good luck!

如果这有帮助,请告诉我们,对不起,我无法提供更多帮助。祝你好运!