我想聚集在一個部門如何使用jQuery獲取所有ID?
<div id="mydiv">
<span id='span1'>
<span id='span2'>
</div>
$("#mydiv").find("span");
ID列表(陣列)給了我一個jQuery對象,而不是真正的數組;
我可以做
var array = jQuery.makeArray($("#mydiv").find("span"));
,然後用一個for循環來把ID屬性到另一個數組
或者我可以做
$("#mydiv").find("span").each(function(){}); //but i cannot really get the id and assign it to an array that is not with in the scope?(or can I)
無論如何,我只是想看看在jQuery中有這樣的簡寫;
是的,我沒有使用真正的數組,因爲在技術上你*能*把重複的ID在HTML即使它完全錯誤做 – 2009-05-05 23:07:33
@Deviant:如果你這麼做了,那麼你已經讓ID沒有用處了......)另一個我的例子不適用於生產代碼的原因就是它沒有過濾元素 - 如果Roy有沒有ID的跨度,最後會得到數組中的空元素。 – Shog9 2009-05-05 23:16:15