jQuery代码在IE8及以下版本中无效

时间:2013-01-15 11:18:49

标签: jquery internet-explorer

我有以下2段代码,这些代码在IE8及以下版本中无效。

services.each(function() {
    $(this).hover(function() {
        services.removeClass('active');
        $(this).addClass('active');                     
    });
});

由于所有4个服务始终具有活动类,因此失败。同样在以下代码中,回调函数不会添加已完成的类。

webform.click(function() {                  
    if(!$(this).hasClass('expanded')) {
            $(this).addClass('expanded');
            $(this).animate({
                marginLeft: '-25%',
                width: "50%",
                minWidth: '400px',
                maxWidth: '700px',
                padding: '0',
                minHeight: "580px",
                height: 'auto',
                borderRadius: "0"
            }, 1000, function () {
                $(this).addClass('completed');
            });
        } 
    });

任何人都可以告诉我如何解决这些问题,更重要的是我将来应该做些什么以确保我的代码与IE兼容。

对于任何有类似jQuery / IE问题的人来说,我在最后一个选项之后放置一个逗号有一个很大的问题,例如在上面的borderRadius: "0"之后,在IE以外的所有浏览器中都会被忽略,这会引发一个问题!< / p>

1 个答案:

答案 0 :(得分:1)

Hover问题 -

hover()有两个参数,一个用于mouseenter,另一个用于mouseleave。如果您只提供一个函数作为参数,它将为两个事件运行。

您不需要each在元素集合上运行方法,jQuery将在内部循环遍历集合的所有元素。

试试hover()

services.hover(function() { 
       /* add on mousenter, remove on mouseleave  */   
        $(this).toggleClass('active');                   

});

使用两个参数的替代方法:

services.hover(function() { 
       /* add on mousenter */   
        $(this).addClass('active');                   

}, function(){
       /*remove on mouseleave */
       $(this).removeClass('active');  
});

API参考:http://api.jquery.com/hover