2012-10-23 72 views
1

我寫了一個jQuery插件,使元素脈衝。它在Chrome和Internet Explorer 9中運行良好。在Internet Explorer 8中,在setTimeout調用後它不工作。jQuery插件:「:懸停」不工作在IE8

我創建了一個的jsfiddle來演示該問題:http://jsfiddle.net/Guykp/ 這裏的javascript代碼:

$(document).ready(function() { 
    $.fn.pulseEffect = function(delay, duration) { 
    var $element, animateOptions; 
    $element = $(this); 
    if (!$element.is(":hover")) { 
     animateOptions = { 
     opacity: $element.css("opacity") === "1" ? .6 : 1 
     }; 
     $element.animate(animateOptions, duration); 
    } 
    return setTimeout((function() { 
     return $element.pulseEffect(delay, duration); 
    }), delay + duration); 
    }; 

    $("#pulse-element").pulseEffect(0, 1000); 
}); 

我怎樣才能使它在Internet Explorer 8的工作?

這是從Internet Explorer 8的錯誤消息:語法錯誤,不能識別的表達式:不支持的僞:懸停

這是解決方案: How do I check if the mouse is over an element in jQuery?

+0

您不能從'setTimeout'內的函數返回值。 –

+1

我知道,但代碼是使用CoffeeScript編寫的。 CoffeeScript返回每個函數中的最後一條語句。但是,謝謝! :-) – Martin

+2

你的代碼有很多問題。首先,'setTimeout'只返回TimeoutHandle。其次,每個人都知道':hover'在網頁瀏覽器中播放不好。而不是'return setTimeout(...);'把它寫成'setTimeout(...); return $ this;' – Christian

回答

2

jQuery沒有:hover選擇器了。某些瀏覽器本地支持:hover,但其他瀏覽器則不支持。 jQuery過去使用它的CSS引擎Sizzle來實現這一點,但它不再這樣做。

嘗試使用hover(或mouseenter/mouseleave)事件。

0

其像

$('li:hover).css('display', 'block');相當於$().css('display', 'block');

像火箭Hazmat說使用jquery hover