我有一个 HTML:
<strong>1)</strong>TEXT THAT I ONLY NEED<p>some par</p><ul>..</ul>
我只需要 “我只需要的文本” 在他的 HTML 中没有任何标签,我怎样才能用 jQuery 得到它?
最佳答案
最好的方法是.clone()
你的对象,.remove()
所有的 .children()
,然后使用 .end()
返回对象最后得到 .text()
.
方法在this blog post中有描述。 .
$("strong")
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text(); //get the text of element
关于jquery - 如何在 HTML 中使用 jQuery 获取不带标签的直接文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8624592/