gpt4 book ai didi

jquery - 用 jquery 去除所有 html 标签?

转载 作者:行者123 更新时间:2023-11-30 23:53:48 25 4
gpt4 key购买 nike

如何去掉jquery中的所有html标签。 PHP 中类似 strip_tags 的函数

例如:有如下内容:

 <div id='test'>This is <b>bold</b> and this <div>is</div> <i>italic</i>.</div>

现在我想将其更改为 <div id='test'>This is bold and this is italic.</div>

最佳答案

使用.text() :

$('#test').text($('#test').text());

我用过.text()两次因为使用 .html()设置内容会导致带小于号和大于号的字符串渲染错误:

Hello &lt;foo&gt; // Set with .text()
Hello <foo> // Set with .html()

如果您使用.html() ,然后<foo>将成为一个标签。

关于jquery - 用 jquery 去除所有 html 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com