简体   繁体   English

使用 jquery 从嵌套的 div 标签中获取所有锚标签文本

[英]Get all anchor tag text from nested div tag using jquery

I want to get all anchor tag links text and push them into array, so all links text will be available into array, I tried using jquery but due to nested div it is unable to get each link text separately我想获取所有锚标记链接文本并将它们推送到数组中,因此所有链接文本都将可用到数组中,我尝试使用 jquery 但由于嵌套 div 无法分别获取每个链接文本

 let data = [] $(".main-class").each((i, elem) => { data.push({ link_text: $(elem).find(".header-text a").text() }) }); console.log(data)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div="main-class"> <div class="upper-class"> <section class="home-item"> <div> some text here</div> <div class="header-text" > <a href="www.xyz.com">Link 1 text</a> </div> <div class="header-text" > <p></p> </div> </section> <section class="home-item"> <div> some text here</div> <div class="header-text"> <a href="www.xyz1.com">Link 2 text</a> </div> <div class="header-text"> <p></p> </div> </section> <section class="home-item"> <div> some text here</div> <div class="header-text"> <a href="www.xyz2.com">Link 3 text</a> </div> <div class="header-text"> <p></p> </div> </section> </div> </div>

The cause is that .find("a").text() will combine all the texts for all the a s into a single text - that's how jquery .text() works.原因是.find("a").text()会将所有a的所有文本组合成一个文本 - 这就是 jquery .text()的工作原理。

You need to "loop" each "a", not using a single .text() call against all of them.您需要“循环”每个“a”,而不是对所有这些“a”使用单个.text()调用。

Keeping to the nearest your code (with the .push ), the simplest way is to change the selector, rather than add an inner .each保持最近的代码(使用.push ),最简单的方法是更改选择器,而不是添加内部.each

    let data = []
    $(".main-class .header-text a").each((i, elem) => {
      data.push({
        link_text: $(elem).text()
      })
    });

This can be shortened to a one-line by using .map which handles the creating of an array and.push for you:这可以通过使用.map为您处理创建数组和 .push 缩短为一行:

let data2 = $(".main-class .header-text a").map((i, elem) => { return { link_text: elem.textContent }}).get();

Updated snippet (including typo fix)更新的片段(包括错字修复)

 let data = [] $(".main-class.header-text a").each((i, elem) => { data.push({ link_text: $(elem).text() }) }); //console.log(data) let data2 = $(".main-class.header-text a").map((i, elem) => { return { link_text: elem.textContent }}).get(); console.log(data2)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="main-class"> <div class="upper-class"> <section class="home-item"> <div> some text here</div> <div class="header-text" > <a href="www.xyz.com">Link 1 text</a> </div> <div class="header-text" > <p></p> </div> </section> <section class="home-item"> <div> some text here</div> <div class="header-text"> <a href="www.xyz1.com">Link 2 text</a> </div> <div class="header-text"> <p></p> </div> </section> <section class="home-item"> <div> some text here</div> <div class="header-text"> <a href="www.xyz2.com">Link 3 text</a> </div> <div class="header-text"> <p></p> </div> </section> </div> </div>

It work, you did mistake here: <div="main-class"> should be <div class="main-class">它工作,你在这里做错了: <div="main-class">应该是<div class="main-class">

EDIT: if you want separate item, you rather use map() ;编辑:如果你想要单独的项目,你宁愿使用map()

 var arr = $(".main-class").find(".header-text a").map(function() { return $(this).text(); }).get(); console.log(arr);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="main-class"> <div class="upper-class"> <section class="home-item"> <div> some text here</div> <div class="header-text" > <a href="www.xyz.com">Link 1 text</a> </div> <div class="header-text" > <p></p> </div> </section> <section class="home-item"> <div> some text here</div> <div class="header-text"> <a href="www.xyz1.com">Link 2 text</a> </div> <div class="header-text"> <p></p> </div> </section> <section class="home-item"> <div> some text here</div> <div class="header-text"> <a href="www.xyz2.com">Link 3 text</a> </div> <div class="header-text"> <p></p> </div> </section> </div> </div>

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

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