Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery和IE,对象不';不支持此属性或方法_Javascript_Jquery_Internet Explorer - Fatal编程技术网

Javascript jQuery和IE,对象不';不支持此属性或方法

Javascript jQuery和IE,对象不';不支持此属性或方法,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,我用这段代码在网站上获得了一个下拉式菜单。 它在除IE以外的所有浏览器上都可以正常工作,甚至在IE上也可以正常工作,除了在这一页上,我得到了“Object不支持此属性或方法”错误 IE告诉我错误在这里,这部分在一个“头”文件中,该文件在页面其余部分之前加载 <script type="text/javascript"> $(document).ready(function(){ $("#nav-one li").hover( function(){ $("u

我用这段代码在网站上获得了一个下拉式菜单。 它在除IE以外的所有浏览器上都可以正常工作,甚至在IE上也可以正常工作,除了在这一页上,我得到了“Object不支持此属性或方法”错误

IE告诉我错误在这里,这部分在一个“头”文件中,该文件在页面其余部分之前加载

<script type="text/javascript"> 
$(document).ready(function(){
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

if(document.all){
    $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    };
});

$.fn.hoverClass = function(c) {
    return this.each(function(){
    $(this).hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
        );
    });
};    
</script> 

$(文档).ready(函数(){
$(“导航一里”)。悬停(
函数(){$(“ul”,this).fadeIn(“fast”);},
函数(){}
);
如果(全部文件){
$(“#nav one li”).hoverClass(“sfHover”);//这一行是错误所在
};
});
$.fn.hoverClass=函数(c){
返回此值。每个(函数(){
$(此)。悬停(
函数(){$(this).addClass(c);},
函数(){$(this).removeClass(c);}
);
});
};    
我不认为错误在代码中,因为它工作正常,除了这一页之外,每个页面上都没有错误,这也是唯一使用额外jQuery代码的页面。 jQuery代码的其余部分在页面上运行良好,只有当鼠标悬停在菜单项上时下拉菜单不起作用。 如果有人能帮我找到答案,我将不胜感激


谢谢,

我认为jQuery正在覆盖这个插件。可能您已经多次在此页面上包含jQuery。试试这个

$(document).ready(function(){

$.fn.hoverClass = function(c) {
    return this.each(function(){
    $(this).hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
        );
    });
};  

    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

if(document.all){
    $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    };
});

调用
hoverClass
时,尚未定义它。您需要在代码顶部声明
$.fn.hoverClass

$.fn.hoverClass = function(c) {
    return this.each(function(){
    $(this).hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
        );
    });
};   
$(document).ready(function(){
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

if(document.all){
    $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    };
}); 

在声明之前,您正在访问
$.fn.hoverClass
。为了安全起见,您应该在任何其他代码之前声明/加载所有插件

另外,在
$.fn.hoverClass
中,您不需要
。每个
,您只需
返回这个.hover()
。(另外,在
if
语句后不需要分号)


什么是hoverClass
?它不是jQuery本身的一部分。您是否需要包含某个插件?它在if语句下定义。它是在脚本本身中创建的
$.fn.hoverClass=
@Willian Van Rensselaer,这是正确的!愚蠢的,讨厌死了!请提交作为答复,我将接受正确的答复!你不需要
。在
hoverClass
中的每个
,你可以
返回这个.hover(function(){}…
。这只是因为
$.fn.hoverClass
在OP中调用时不存在。@Esailija-为什么不存在?如果OP正在添加它(再次检查代码)。如果(document.all)删除
If
并尝试在任何浏览器中运行代码,它将失败,因为尚未分配
$.fn.hoverClass
。按原样,它将在其他浏览器中工作,因为它们没有document.all和if块不会执行。
TypeError:Object[Object Object Object]在google Chromete中没有方法“hoverClass”
。插件添加到$(document.ready()之外。只要它被添加到外部,它在之前或之后都没有关系。此外,OP还提到了IE中其他浏览器中运行的相同代码。只有在这个页面上,它才会给出该错误。因此,重新查看注释+1是有意义的,因为缩进更准确,因此不太容易混淆
$.fn.hoverClass = function(c) {
    return this.hover( 
        function() { $(this).addClass(c);  },
        function() { $(this).removeClass(c); }
    );
}; 

$(document).ready(function(){
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );

    if(document.all){
        $("#nav-one li").hoverClass("sfHover");//THIS LINE IS WHERE THE ERROR IS
    }
});