无法从循环数据属性的内容中获取索引的值

Can't get the value of the index from the contents of a looped data attribute

本文关键字:获取 索引 循环 数据属性      更新时间:2023-09-26

下面是我的片段,因此为了锐化细节,首先我遍历当前单击的元素具有的每个数据属性,获取内容并按"%"将其拆分为分隔符并循环每个拆分的内容。现在,我试图做的是提醒索引为"0"的内容(您可以在"if"语句中看到,如果索引"0"则提醒索引[0]值),但似乎不起作用,甚至没有传递提供的if语句。任何想法,线索,建议,建议,请帮忙?

$(document).ready(function(){
  $("button").click(function(){
  $.each($(this).data(),function(index,value){
       if(index !== "plugin_ripples"){
           var classList = value.split("%");
            $.each(classList, function(index, item) {
              alert("index: "+index+" index value: "+item);
               if(index[0]){
                   alert(index[0].item);
               }
            });
       }
    });
    
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-name="test%jason%kayko" data-test="test 2">test</button>

由于没有人就我想要实现的目标给我不同的方法,所以我为自己创建了一个不同的方法来满足我的需求和要求,我所做的是将其与数组连接起来,请查看下面的代码片段。

$(document).ready(function(){
  $("button").click(function(){
  $.each($(this).data(),function(index,value){
       if(index !== "plugin_ripples"){
           var classList = value.split("%");
           var data_items = [];
            $.each(classList, function(index, item) {
              data_items.push(item);
            });
            if(data_items[0] === "null"){
             alert(data_items[0]); 
            }
       }
    });
    
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-name="test%jason%kayko" data-test="test 2">test</button>