简体   繁体   English

"jQuery:如何遍历具有数据属性的元素"

[英]jQuery: how to loop through elements with data attribute

I have several divs that looks like this:我有几个看起来像这样的 div:

<div class='popupDiv' data-layergroup='layer1'>divcontent 1</div>
<div class='popupDiv' data-layergroup='layer1'>divcontent 2</div>
<div class='popupDiv' data-layergroup='layer2'>divcontent 3</div>
<div class='popupDiv' data-layergroup='layer2'>divcontent 4</div>

I'm a bit stumped as to how to loop through all popupDiv divs, and then loop through each layergroup separately.对于如何遍历所有 popupDiv div,然后分别遍历每个 layergroup,我有点难过。 I want to end with a single array for each layergroup.我想以每个图层组的单个数组结束。 I'd need something like:我需要类似的东西:

var mainArray = [];
$('.popupDiv').each(function(){
    var tempArray = [];
    $([unique layer value]).each(function(){
        // Put div values from layergroup in tempArray
    });
    mainArray.push(tempArray);
});
return mainArray;

But I don't know the syntax I'm looking for.但我不知道我正在寻找的语法。 What do I do?我该怎么办?

<div class='popupDiv' data-layer='layer1'></div>
<div class='popupDiv' data-layer='layer1'></div>
<div class='popupDiv' data-layer='layer2'></div>
<div class='popupDiv' data-layer='layer2'></div>

Loop through the elements 循环遍历元素

$('.popupDiv[data-layer]').each(function(){

});

to loop through each group seperately, you can use below logic 单独循环遍历每个组,您可以使用以下逻辑

 //create an array to store processed data-layer type
 var dataArray = new Array();
    $('.popupDiv').each(function(){
      var dataLayer = $(this).data('layer');
      //check if data-layer already processed
      if(!dataArray.indexOf(dataLayer))
      {
         //update data array
         dataArray.push(dataLayer);
         $('.popupDiv[data-layer="'+ dataLayer +'"]').each(function(){
            //do your stuff here
         });
      }
    });

You can loop through each of the div having attribute 'data-layer' as follows: 您可以循环遍历具有属性“data-layer”的每个div,如下所示:

$('.popupDiv').each(function(i) {
        if ($(this).attr('data-layer') == 'layer' + i + 1) {

            $(this).each(function() {
                alert($(this).attr('data-layer'));
                //change this to whatever you want
            });
        }
    });

So this will check for 'layer1', 'layer2' and so on. 所以这将检查'layer1','layer2'等等。

You do not need two each loops here. 这里你不需要两个循环。 You can use Has Attribute Selector . 您可以使用“ 具有属性选择器” you are also having duplicate IDs for divs. 你也有div的重复ID。 IDs should be unique, use class name instead: ID应该是唯一的,请改用类名:

$('[data-layergroup]').each(function(){
    // Do stuff with each div
    console.log($(this).data('layergroup'));//current data layer value
});

For iterating over the values(FYI-BAD APPROACH): 迭代值(FYI-BAD APPROACH):

$.each($("[data-layergroup]").map(function() {  return $(this).data('layergroup');}).get(), function(index, item) {
   // item
});

use class instead of id: 使用class而不是id:

<div class='popupDiv' data-layer='layer1'></div>
<div class='popupDiv' data-layer='layer1'></div>
<div class='popupDiv' data-layer='layer2'></div>
<div class='popupDiv' data-layer='layer2'></div>

Then you can loop each layer seperatly: 然后你可以分别循环每一层:

for (var i = 1; i <= 2; i++) {
  $(".popupDiv[data-layer|='layer"+i+"']").each(function(){
       // do stuff with layer i
   });
}

here is another brain teaser - what if you have a variable for the element name and variable for the data attribute:这是另一个脑筋急转弯 - 如果您有元素名称的变量和数据属性的变量怎么办:

like so:像这样:

 let classname = 'aaa', tot = 2, cn; if (classname=='aaa') cn = $('.aaa'); else if (classname=='bbb') cn = $('.bbb'); for (let ps=0;tot>ps;ps++) { $(cn.find("[data-layer='"+ps+"']")).attr('data-layer2'); }<\/code><\/pre>

it does not work, any reason?它不起作用,有什么原因吗?

"

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

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