Jquery-嵌套元素循环

问题描述 投票:0回答:1

我有多个具有相同类名(listing_class的div ..我需要分别获取所有div中每个锚标记的href值。]]

var length = $(".listing_class").length;

for (var i = 0; i < length; i++) {
  $(".listing_class").each(function() {
    console.log($(this).children('a').attr('href'));
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="listing_class " style="display: none;">
  <a href="?section=all&status=active" class="active"> All Listings</a>
  <a href="?section=sale&status=active" class="active"> For Sale (0)</a>
  <a href="?section=rent&status=active" class="active"> For Rent (0)</a>
</div>
<div class="listing_class " style="display: none;">
  <a href="?section=a&status=p" class="active"> a</a>
  <a href="?section=b&status=p" class="active"> b</a>
  <a href="?section=c&status=p" class="active"> c</a>
</div>
<div class="listing_class " style="display: none;">
  <a href="?section=QQ&status=FF" class="active"> a</a>
  <a href="?section=FF&status=FF" class="active"> b</a>
  <a href="?section=VV&status=FF" class="active"> c</a>
</div>
<div class="listing_class " style="display: none;">
  <a href="?section=WW&status=p"> VV</a>
  <a href="?section=WW&status=p"> CC</a>
  <a href="?section=WW&status=p"> AQ</a>
</div>
<div class="listing_class " style="display: none;">
  <a href="?section=A&status=p"> VV</a>
  <a href="?section=B&status=p"> CC</a>
  <a href="?section=X&status=p"> AQ</a>
</div>

问题是,它仅返回第一个div的锚点值,并且不会进一步循环……在此先感谢

我有多个具有相同类名(diving_class)的div。我需要分别获取所有div中每个锚标记的href值。var length = $(“。listing_class”)。length;对于(var ...

jquery each
1个回答
2
投票

[基本上,更好的解决方案是找到a类下的所有.list_class元素。使用下面的jQuery选择器$('.listing_class a'),您可以获得可以迭代使用的所有a标签。

© www.soinside.com 2019 - 2024. All rights reserved.