2011-08-17 36 views
2

我想問一下編寫這個JQUERY代碼的最佳方法。 我有DOM結構是這樣的:JQUERY Refined Selection

<div id="chatCenterMembers"> 
    <div id="adam" class="chatmember 100"> 
     <div class="newchatmessage" style="display: block;"></div> 
    </div> 
    <div id="steven" class="chatmember 101"> 
     <div class="newchatmessage"></div> 
    </div> 
</div> 

我想提取用戶標識的列表 - 如:100,101類名稱爲數組,其中帶班「newchatmessage」的DIV有樣式設置爲顯示塊 - 例如, :它的可見性。

我可以用$ .each循環做,但效率不高。

有沒有更好的方法? THX

+0

首先,你有2個IDS設置爲 「用戶名」。如果您想多次使用此ID,請改爲使用class。 – Elorfin

+0

用戶名只是通用的...會看起來像這樣的現場代碼...有沒有一種方法來爲newchatmessage和顯示塊集提取useridXX? – Adam

回答

1

你可以收集所有需要可見的元素就是這樣:

$(".chatmember .newchatmessage").filter(":visible").each(function() 
{ 
    //Extract here any information you want, i.e. 
    //this.className.match(/[0-9]+/) 
}); 
0

試試這個代碼:

$('.newchatmessage').map(function(){ 
if ($(this).css('display') == 'block') 
    return $(this).parent().attr('class').replace('chatmember ',''); 
}).get();