简体   繁体   English

删除标签(和标签内容)但保留文字

[英]Remove tags (and tag content) but leave text

Given HTML of this form: 给出这种形式的HTML:

<div>
    <a href="#">Generic Anchor</a>
    <li> Some text </li>
    <b> More stuff </b>
    Just text.
</div>

Where the tags are random, is there a way in jQuery (or Javascript) to reduce it to ONLY the text, meaning 标签是随机的,在jQuery(或Javascript)中有一种方法可以将它简化为仅文本,意思是

<div>
    Just text.
</div>

Without having to specify particular tags to strip (I'm aware of .text(), but I don't want the contents of the tags I'm removing). 无需指定要剥离的特定标记(我知道.text(),但我不想要删除标记的内容)。

Try this: http://jsfiddle.net/5SRLE/1/ 试试这个: http//jsfiddle.net/5SRLE/1/

$("div").children().remove();

Of course, you'll want to target the specific div with that selector (if you have other divs that you don't want to remove children from). 当然,您需要使用该选择器来定位特定div (如果您有其他不希望从中删除子项的div)。 If you have problems, show your context so we can make it more specific... or give your div an id like myDiv and then use the selector $("#myDiv") in place of $("div") 如果你有问题,请显示你的上下文,以便我们可以使它更具体......或者给你的div像myDiv这样的id然后使用选择器$("#myDiv")代替$("div")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM