jQuery循环遍历动态元素并获取数据值

2nbm6dog  于 2023-06-22  发布在  jQuery
关注(0)|答案(2)|浏览(131)

我正在尝试一些东西来完成我的要求与可折叠面板

$(".sport").on("click", function() {
  var thisId = $(this).attr("id");
  var thisChildren = $(this) + ".sportlist";
  $(thisChildren).each(function(index) {
    
  });
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" />

<div style="height:600px;padding:0px;margin:0px;">

  <div class="col-md-3 scroll-container" style="padding:0px;">
    <div class="panel-group" id="accordionMenu" role="tablist" aria-multiselectable="true">
      <div class="panel panel-default">
        <div class="panel-heading" role="tab">
          <h4 class="panel-title">
            <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse1" aria-expanded="true">
              <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Cricket (2 items)
            </a>
          </h4>
        </div>
        <div class="panel-collapse collapse" id="collapse1" role="tabpanel" style="height: 0px;">
          <div class="sportlist" data-value="1">
            &nbsp;&nbsp; Sachin
          </div>
          <div class="sportlist" data-value="2">
            &nbsp;&nbsp; Kohli
          </div>
        </div>
        <div class="panel-heading" role="tab">
          <h4 class="panel-title">
            <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse2" aria-expanded="true">
              <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Other (2 items)
            </a>
          </h4>
        </div>
        <div class="panel-collapse collapse" id="collapse2" role="tabpanel" style="height: 0px;">
          <div class="sportlist" data-value="3">
            &nbsp;&nbsp; Bob
          </div>
          <div class="sportlist" data-value="4">
            &nbsp;&nbsp; Willson
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

我需要的是当我点击Cricket时,我需要将data-value存储到数组中。我试过了但没成功

$(".sport").on("click", function() {
  var thisId = $(this).attr("id");
  var thisChildren = $(this) + ".sportlist";
  $(thisChildren).each(function(index) {
    
  });
});
5n0oy7gb

5n0oy7gb1#

使用来自ahref沿着$.map

$(".sport").on("click", function() {
  let id = $(this).attr("href")
  , list = $.map($(id).find(".sportlist"), function(item){
    return $(item).data("value")      
  })
  console.log(list)
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" />

<div style="height:600px;padding:0px;margin:0px;">

  <div class="col-md-3 scroll-container" style="padding:0px;">
    <div class="panel-group" id="accordionMenu" role="tablist" aria-multiselectable="true">
      <div class="panel panel-default">
        <div class="panel-heading" role="tab">
          <h4 class="panel-title">
            <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse1" aria-expanded="true">
              <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Cricket (2 items)
            </a>
          </h4>
        </div>
        <div class="panel-collapse collapse" id="collapse1" role="tabpanel" style="height: 0px;">
          <div class="sportlist" data-value="1">
            &nbsp;&nbsp; Sachin
          </div>
          <div class="sportlist" data-value="2">
            &nbsp;&nbsp; Kohli
          </div>
        </div>
        <div class="panel-heading" role="tab">
          <h4 class="panel-title">
            <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse2" aria-expanded="true">
              <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Other (2 items)
            </a>
          </h4>
        </div>
        <div class="panel-collapse collapse" id="collapse2" role="tabpanel" style="height: 0px;">
          <div class="sportlist" data-value="3">
            &nbsp;&nbsp; Bob
          </div>
          <div class="sportlist" data-value="4">
            &nbsp;&nbsp; Willson
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
amrnrhlw

amrnrhlw2#

下面是另一种不使用id的方法

$(".sport").click(function () {
   let ids = $.map($(this).closest(".panel-heading").next(".panel-collapse").find(".sportlist"), function (element) {
        return $(element).data("value")
    });
    console.log(ids)
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" />
<div style="height:600px;padding:0px;margin:0px;">
    <div class="col-md-3 scroll-container" style="padding:0px;">
        <div class="panel-group" id="accordionMenu" role="tablist" aria-multiselectable="true">
            <div class="panel panel-default">
                <div class="panel-heading" role="tab">
                    <h4 class="panel-title">
                        <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse1" aria-expanded="true">
                            <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Cricket (2 items)
                        </a>
                    </h4>
                </div>
                <div class="panel-collapse collapse" id="collapse1" role="tabpanel" style="height: 0px;">
                    <div class="sportlist" data-value="1">
                        &nbsp;&nbsp; Sachin
                    </div>
                    <div class="sportlist" data-value="2">
                        &nbsp;&nbsp; Kohli
                    </div>
                </div>
                <div class="panel-heading" role="tab">
                    <h4 class="panel-title">
                        <a class="sport collapsed" role="button" data-toggle="collapse" data-parent="#accordionMenu" href="#collapse2" aria-expanded="true">
                            <small><i class="more-less glyphicon glyphicon-chevron-right"></i></small> &nbsp;Other (2 items)
                        </a>
                    </h4>
                </div>
                <div class="panel-collapse collapse" id="collapse2" role="tabpanel" style="height: 0px;">
                    <div class="sportlist" data-value="3">
                        &nbsp;&nbsp; Bob
                    </div>
                    <div class="sportlist" data-value="4">
                        &nbsp;&nbsp; Willson
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

jQuery closes-对于集合中的每个元素,通过测试元素本身并在DOM树中向上遍历其祖先来获取第一个匹配选择器的元素。
jQuery next-获取匹配元素集合中每个元素的紧接着的兄弟。如果提供了一个选择器,则仅当下一个同级与该选择器匹配时,它才会检索该同级。
jQuery find-获取当前匹配元素集合中每个元素的后代,通过选择器、jQuery对象或元素进行过滤。
jQuery map-通过函数传递当前匹配集中的每个元素,生成一个包含返回值的新jQuery对象。

相关问题