jquery - 如何在 HTML 中使用 jQuery 获取不带标签的直接文本

标签 jquery html

我有一个 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/

相关文章:

javascript - 如何找到最后单击的动态按钮

javascript - IDBCursor 对 ajax 成功有不同的值(value)

javascript - 如何触发多远程 jquery 自动完成 UI 的自动搜索选项

javascript - AngularJS ng-repeat 将 css 应用于整个列表

javascript - knockoutjs 主页面调用组件

javascript - jQuery 按键和值对 JSON 进行计数(这个函数可以工作吗?)

html - CSS 向左浮动并位于图像下方

html - CSS伪元素 block 引用

html - 如何使用 css 创建棋盘格图案的棋盘?

php - 服务器上的欺骗时间