gpt4 book ai didi

javascript - 如何使用 jquery 获取第 n 个子值

转载 作者:行者123 更新时间:2023-11-30 09:28:19 24 4
gpt4 key购买 nike

我有一个像这样的无序列表:

<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
<div id='output'></div>

我刚刚阅读了关于 javascript 中的第 nth-child 的内容,我有一个小问题:如何在输出 div 中打印出第 nth-child 的值?我这样做并返回 [object HTMLDivElement]

var el=$("ul li:nth-child(2)").val();
$('#output').text('The second child is: '+el[0]);
console.log(el);

最佳答案

您可能想要获取文本值:

var el = $("ul li:nth-child(2)").text();

然后像这样输出:

 $('#output').text('The second child is: ' + el);

关于javascript - 如何使用 jquery 获取第 n 个子值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971302/

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