简体   繁体   English

如何遍历div数组以显示()?

[英]How can I loop through an array of divs to show() them?

first post.. experienced with programming but not so much jQuery/javascript. 第一篇文章..有编程经验,但没有太多jQuery / javascript。 I have some divs that are hidden at first, and they are assigned id's in ascending order: 我首先隐藏了一些div,并且为它们分配了ID的升序:

  <script>
    $(document).ready(function() {
      $(".over_map").each(function(i) {
          $(this).attr('id', "over_map" + (i + 1));
          // console.log($(this));
      });
    });
  </script>

Now I want to loop through a dynamic number of these divs and show them. 现在,我想遍历这些div的动态数量并显示它们。 If (eventually, not implemented yet) an SQL call returns 4 records, I would want to show() 4 of the hidden divs. 如果(最终尚未实现)SQL调用返回4条记录,则我想对隐藏的div进行show()4条记录。 Seems like this should be simple but I am missing something.. limited internet access unfortunately, but I didn't find any stack overflow entries pertaining to it.. any help appreciated! 看来这应该很简单,但我缺少一些东西。不幸的是,互联网访问受到限制,但是我没有找到任何与此有关的堆栈溢出条目。

If you have a set of elements in a jQuery object, you don't need to loop them at all - just call .show() on the set. 如果jQuery对象中有一组元素,则根本不需要循环它们-只需在该集合上调用.show()

eg 例如

$('#over_map1, #over_map3').show();

If you have a list of ids that you want to show, you can build the selector string: 如果您有要显示的ID列表,则可以构建选择器字符串:

function buildSelector(ids) {
  var selectors = [];
  for (var i = 0; i < ids.length; i++) {
    selectors.push("#over-map" + ids[i]);
  }
  return selectors.join(", ");
}

$(document).ready(function(){
  var ids = [1, 3];
  $(buildSelector(ids)).show();
});

Demo at http://codepen.io/precise54/pen/nkyzg 演示在http://codepen.io/precise54/pen/nkyzg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM