如何在HTML中只使用jQuery获取没有标签的直接文本 [英] How to get only direct text without tags with jQuery in HTML

查看:378
本文介绍了如何在HTML中只使用jQuery获取没有标签的直接文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML:

 < strong> 1)< / strong> TEXT THAT I ONLY NEED< ; p>一些par< / p>< ul> ..< / ul> 

我只需要TEXT THAT I ONLY NEED HTML中的任何标签,我怎样才能用jQuery获得它?

最好的方法是 .clone() 您的对象, .remove() 它的所有 .children() ,然后使用 .end() ,最后得到 .text()



该方法在此博客文章

  $(strong)
.clone()//克隆元素
。儿童() //选择所有子元素
.remove()//删除所有子元素
.end()//再次返回选定元素
.text(); //获取元素文本


I've got an HTML:

<strong>1)</strong>TEXT THAT I ONLY NEED<p>some par</p><ul>..</ul>

I need only "TEXT THAT I ONLY NEED" that is not withing any tags in his HTML, how can I get it with jQuery?

解决方案

The best way is to .clone() your object, .remove() all of its .children(), then go back to the object using .end() and finally get the .text().

The method is described in 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

这篇关于如何在HTML中只使用jQuery获取没有标签的直接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆