2009-10-26 68 views
1

這個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

回答

2

它工作在Firefox和IE7的罰款 - 這是一個Working Demo與您的代碼。

karim79是對在siblings()將讓你的集合,而是鏈接到attr()siblings()讓你的第一個元素在匹配集的指定屬性。由於<a>notes類只有一個兄弟姐妹在這個例子中,那麼這適用於這種情況下的預期。如果你想獲得包裝集合中所有兄弟的類屬性,那麼你需要以某種方式迭代它們,最有可能使用each()map()

+0

是的,它在IE7中工作。事實證明,還有其他的東西使得這個不能正常工作。謝謝 – 2009-10-26 16:17:43

1

大概是因爲兄弟姐妹()將讓你的集合,所以你需要遍歷匹配的元素:

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