gpt4 book ai didi

jquery - 如何在jquery中查找元素的文本

转载 作者:行者123 更新时间:2023-12-01 07:19:49 25 4
gpt4 key购买 nike

你好,我有这样的 html 标记:

<div class="expandNode expand">
<input id="100" type="checkbox" value="100" data-parent="">
<span>Office/Commercial</span>
</div>

在控制台中,当我执行 $('#100') 时,我得到:

[<input id=​"100" type=​"checkbox" value=​"100" data-parent>​]

我需要的是跨度内的文本,即办公/商业。

我该怎么做?

最佳答案

尝试:

$('#100').next('span').text(); 

或者像:

$('.expandNode span').text();   // not suggested

此外,输入标签需要关闭,但我仍然不建议使用数字作为 ID 元素( < HTML5 )

实现这一目标的更灵活且面向 future 的方法是:

$('#100').closest('.expandNode').find('span').text();

此外,如果您确定只有一个 <span>在父元素中,您可以选择:

$('#100').siblings('span').text(); 

这样你就不必依赖 span立即.next()元素。

关于jquery - 如何在jquery中查找元素的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15056524/

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