我有这个 HTML 代码:
<div>
<span>
First text
<span class="dot"></span>
Second text
</span>
<span class="other-span">
</span>
</div>
我只需要获取第二条文本。我试过这个 JQuery 代码:
const a = $('div').find('span:nth-child(1)').text();
console.log(a);
上面jquery代码的输出是这样的:
First text Second text
在 span 类 dot 元素之后只得到 Second text 正确的做法是什么
最佳答案
您可以获得 HTML,然后像这样剥离文本:
const a = $('div').find('span:nth-child(1)').html().split('<span class="dot"></span>')[1];
console.log(a.trim());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<span>
First text
<span class="dot"></span> Second text
</span>
<span class="other-span">
</span>
</div>
关于javascript - 使用 jquery 获取 span 元素之后的文本,不包括 span 之前的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53944199/