gpt4 book ai didi

jquery - 尝试使用 jQuery 从

中获取字符串

转载 作者:行者123 更新时间:2023-12-01 02:35:19 26 4
gpt4 key购买 nike

我正在尝试使用 jQuery 获取下面的字符串 Test123:

<div id="pri_txt"><p>Test123</p></div>

我用过:

var str = $('#pri_txt').next('p').text();

但是好像不行。我错过了什么吗?

最佳答案

关闭。 next() 将选择下一个兄弟元素。您要查找的是其子 p 标记。使用descendant selector jQuery('祖先后代')

Selects all elements that are descendants of a given ancestor.

var str = $('#pri_txt p').text();
<小时/>

如果您要查找第一段,请使用 :first 选择器

var str = $('#pri_txt p:first').text();
<小时/>

如果您要搜索特定段落,请使用 :eq(index) 选择器。其中,index 是您要查找的元素相对于父元素的从 0 开始的索引:

var str = $('#pri_txt p:eq(10)').text();

关于jquery - 尝试使用 jQuery 从 <p> 中获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7303287/

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