替代.is(":hover")?在Opera中不起作用

时间:2012-08-02 04:40:20

标签: javascript jquery javascript-events hover opera

请参阅http://jsfiddle.net/cgWdF/3/

除了最新的歌剧之外,每个浏览器都可以正常工作。

*未在IE9以下测试

应该指定,它需要返回true或false,我不是用它来绑定事件。

2 个答案:

答案 0 :(得分:0)

您可以使用.mouseover()和.mouseout()来实现相同的效果。如果你想延迟动画,你可以在jQuery UI中使用.animate()

$('#sample').mouseover(
        function() {
            $(this).stop().animate({
                backgroundColor: "yellow"}, 200);
        });

$('#sample').mouseout(
        function() {
            $(this).stop().animate({
                backgroundColor: "#aaa"}, 200);
        });

http://jsfiddle.net/cgWdF/8/

答案 1 :(得分:0)

从jQuery 1.9.1开始,其他浏览器已经赶上了Opera - 它现在不再适用于它。正如你的小提琴所调查的那样,“它”是.is(“:悬停”)。

我为.is(“:hover”)写了一个解决方法,请参阅小提琴http://jsfiddle.net/mathheadinclouds/BxL4w/

function mouseIsOver(what){
    return $(what).is(":hover");
}
function mouseIsOverWorkaround(what){
    var temp = $(what).parent().find(":hover");
    return temp.length == 1 && temp[0] == what;
}
function mo(what){
    return document.getElementById("workaround").checked ? mouseIsOverWorkaround(what) : mouseIsOver(what);
}
setInterval(function(){
    var theBox = $("#theBox");
    if(mo(theBox[0])) {
       theBox.css("background", "yellow");
    } else {
       theBox.css("background", "");
    }
}, 200);

和html

<input type="checkbox" id="workaround"/>
<div id="theBox"></div>