![](/img/trans.png)
[英]How can I get the contents of all elements with a specific parent class in jQuery and pass them in a GET variable?
[英]How can I get an array of all elements within a specific class using javascript and/or jQuery?
我正在嘗試使用類“sampleclass”獲取所有元素的數組。 例如,有了我的文檔,我有三個div:
<div class="aclass"></div>
<div class="sampleclass"></div>
<div class="anotheraclass"></div>
<div class="sampleclass"></div>
我想使用javascipt和/或jQuery獲取一個包含“sampleclass”中所有元素的數組。
關於如何做到這一點的任何想法?
$( '.sampleclass' );
然后,您可以使用each
數組遍歷數組。
......
$('.sampleclass').........
另外,你可以遍歷使用它在each
這樣的:
$('.sampleclass').each(function(){
// more code........
})
最后,你也可以得到這樣的每個項目:
$('.sampleclass')[0]; // first
$('.sampleclass')[1]; // second
// and so on........
擴大Jacob Relkin的答案:
$('.sampleClass').each(function()
{
// do something with it...
$(this).css('background-color', 'green');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.