gpt4 book ai didi

javascript - 如何获取相同类名的每个值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:24 25 4
gpt4 key购买 nike

我有一些具有相同类的标签,我想获取它们的每个值并将其附加到一个 div 如何做到这一点

<p class="adr">location1</p>
<p class="adr">location2</p>
<p class="adr">location3</p>
<p class="adr">location4</p>

for (i=1;i<$(".adr").length;i++) {
$("#test").append($(".adr").html() + "</br>");
}

结果:

位置1

位置1

位置1

位置1

好像apppend了4次first class,如何得到1 and 2 and 3 and 4?

最佳答案

在 jquery 中使用 each 获取所有 adr 类的文本。不要逐行append,因为它需要更多的执行时间。尝试作为一个整体追加,希望这有帮助

var str=''
$('.adr').each(function(e){
str+=$(this).text()+ "<br>"
})
$("#test").html(str)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="adr">location1</p>
<p class="adr">location2</p>
<p class="adr">location3</p>
<p class="adr">location4</p>
<div id =test></div>

关于javascript - 如何获取相同类名的每个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55877771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com