开发者

jquery How to loop through a series of items and take action?

开发者 https://www.devze.com 2023-02-06 23:03 出处:网络
Given HTML like: <div clas开发者_开发知识库s=\"itemList\"> <div class=\"streamBox version-2\"> stuff </div>

Given HTML like:

<div clas开发者_开发知识库s="itemList">
  <div class="streamBox version-2"> stuff </div>
  <div class="streamBox version-2"> stuff </div>
  <div class="streamBox version-3"> stuff </div>
  <div class="streamBox version-2"> stuff </div>
  <div class="streamBox version-3"> stuff </div>
  <div class="streamBox version-1"> stuff </div>
</div>

How can I get jquery to loop through all the '.streamBox' inside of the '.itemList' div?

When looping through, given a version #, like 2, how to get it to show() version-2 but hide all the others?

Thanks


$('.itemList').children().hide().filter('.version-2').show();

Also, use the UL element:

<ul class="itemList">
  <li class="version-2"> stuff </li>
  <li class="version-2"> stuff </li>
  <li class="version-3"> stuff </li>
  <li class="version-2"> stuff </li>
  <li class="version-3"> stuff </li>
  <li class="version-1"> stuff </li>
</ul>


$('.itemList').children('.steamBox').each(function(index) {
    // Now you can use $(this) to manipulate a list item
    if (index != 2) // index starts at 0
        $(this).hide();
});


try something like:

function showVersion(ver) {
$('.streamBox').hide();
$('.version-' + ver).show();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消