Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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
Jquery悬停不够快_Jquery - Fatal编程技术网

Jquery悬停不够快

Jquery悬停不够快,jquery,Jquery,当我离开选择器时,如果我移动过快,将不会发生取消覆盖: $("#item").hover(function(){ $(this).addClass("hover"); }function(){ $(this).removeClass("hover"); }); 在创建项目之前,可以使用.live设置EventHandler 或 或 我无法复制它。在Firebug的这一页中写下: $("p").hover(function() { $(this).html('a')

当我离开选择器时,如果我移动过快,将不会发生取消覆盖:

  $("#item").hover(function(){

  $(this).addClass("hover");

  }function(){

  $(this).removeClass("hover");

  });

在创建项目之前,可以使用.live设置EventHandler


我无法复制它。在Firebug的这一页中写下:

$("p").hover(function() { $(this).html('a') }, function() { $(this).html('b') });
即使我移动得很快,也能用。可能是因为您在代码中的第二个函数之前忘记了逗号?您使用什么浏览器?

这里有一个解决方案:

CSS:

Javascript:

if ($.browser.msie) { // I'm not sure if IE7 or 8 support :hover yet
    $('#item').hover(
        function() { $(this).addClass('hover'); },
        function() { $(this).removeClass('hover'); }
    );
}
此代码使您的edge case更薄


具有良好CSS支持的体面浏览器不应该仅仅因为IE有缺陷就因为Javascript调用而放慢速度。如果有人坚持使用IE,那么他们仍然会得到悬停效果,但有时它可能会坚持。你真的那么在乎这些人吗?

悬停卡滞的问题取决于你使用的浏览器。浏览器越新,javascript引擎的速度就越快。调用jQuery库的方式也会有所不同(我建议要么调用库的本地副本,要么使用GoogleLibraries API)。随着库的每一个新版本的发布,它会变得越来越快,因为它们会精简代码并提高效率,所以我建议调用最新版本

使用jQuery 1.4.2及更高版本,可以调用
.toggle()
函数,而不是指定hover函数中包含的鼠标函数。您对代码所做的操作基本上与
.hover(mouseover,mouseout)
(即其基本形式)相同。您调用的每个函数(使用
.removeClass
.addClass
)都在调用每个鼠标操作

使用此选项将加快您的代码速度,因为已优化切换以替换鼠标功能:

jQuery(document).ready(function($) {
  $('#item').hover(function() {
    $(this).toggleClass('hover');
  });
});
编辑:


但是,如果您希望为
.hover()
函数添加动画或指定属性,那么您将像在原始代码中一样调用每个鼠标函数。尽管您也可以使用我上面使用的代码,并使用
.toggleClass()中的函数

如何使用
live
?这与OP中的代码有什么不同?@nick:我没有给出live的示例(那里有一个“或”)。两个答案都不同,一个使用mouseEnter和mouseLeave,另一个使用toggleClass。
#item:hover, #item.hover {
    background-color: #f0f;
}
if ($.browser.msie) { // I'm not sure if IE7 or 8 support :hover yet
    $('#item').hover(
        function() { $(this).addClass('hover'); },
        function() { $(this).removeClass('hover'); }
    );
}
jQuery(document).ready(function($) {
  $('#item').hover(function() {
    $(this).toggleClass('hover');
  });
});