【问题标题】:jQuery - Loop through nested elementsjQuery - 遍历嵌套元素
【发布时间】:2020-04-19 23:36:24
【问题描述】:

我有多个具有相同类名 (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 的锚值并且不会进一步循环。

提前致谢。

【问题讨论】:

    标签: javascript jquery each


    【解决方案1】:

    基本上更好的解决方案是找到.list_class 类下的所有a 元素,如果您使用以下jQuery 选择器$('.listing_class a') 找到所有a 标记。然后这可以用于遍历所有必要的项目并获取每个元素的href 属性。

    也许您可以尝试以下方法:

    $('.listing_class a').each(function(lc) {
      console.log($(this).attr('href'));
    });
    <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>
    
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"
            integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
            crossorigin="anonymous"></script>

    我希望这会有所帮助!

    【讨论】:

    • @Coffee 太棒了,乐于助人!如果解决方案有效,您能否考虑接受作为答案?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2011-04-10
    • 2016-07-29
    • 2015-05-16
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多