jQuery兄弟姐妹()/ next()在IE中不起作用

问题描述:

这个jQuery脚本适用于firefox,它会给出警报。 但它在IE 7中不起作用。有什么想法出了什么问题?jQuery兄弟姐妹()/ next()在IE中不起作用

标记:

<td class="tdnotes"> 
    <a title="" class="notes" id="order-1" href="#"> 
    <!-- an image --> 
    </a>        
    <div style="display: none;" id="wrap-order-1" class="thenotes"> 
    <!-- text area, input elements --> 
    </div> 
</td> 

脚本:

$(function(){ 

    $("a.notes").click(function() { 
     alert($(this).siblings().attr("class")); 
     // I have tried .next() but didn't work as well 
     // alert($(this).next().attr("class")); 
     return false; 
    }); 

    }); 
+0

你是在ready()还是load() ? – cletus 2009-10-26 10:28:45

它工作在Firefox和IE7的罚款 - 这是一个Working Demo与您的代码。

karim79是对在siblings()将让你的集合,而是链接到attr()siblings()让你的第一个元素在匹配集的指定属性。由于<a>notes类只有一个兄弟姐妹在这个例子中,那么这适用于这种情况下的预期。如果你想获得包装集合中所有兄弟的类属性,那么你需要以某种方式迭代它们,最有可能使用each()map()

+0

是的,它在IE7中工作。事实证明,还有其他的东西使得这个不能正常工作。谢谢 – 2009-10-26 16:17:43

大概是因为兄弟姐妹()将让你的集合,所以你需要遍历匹配的元素:

$("a.notes").click(function() { 
    $(this).siblings().each(function() { 
     alert($(this).attr('class')); 
    }); 
    return false; 
});