javascript - jQuery(几乎)相当于 PHP 的 strip_tags()

标签 javascript jquery html strip-tags

这个函数有 jQuery 版本吗?

string strip_tags( string $str [, string $allowable_tags ] )

从一个字符串中去除所有标签和其中的内容,允许的标签字符串中定义的除外。

喜欢:

var stripped = strip_tags($('#text').html(), '<p><em><i><b><strong><code>');

来自:

<div id="text">
  <p> paragraph </p>
  <div> should be stripped </div>
</div>

最佳答案

要删除标签而不是内容,这是 PHP 的 strip_tags() 的行为方式,您可以这样做:

var whitelist = "p"; // for more tags use the multiple selector, e.g. "p, img"
$("#text *").not(whitelist).each(function() {
    var content = $(this).contents();
    $(this).replaceWith(content);
});

Try it out here.

关于javascript - jQuery(几乎)相当于 PHP 的 strip_tags(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5601903/

相关文章:

javascript - 在 OWL Carousel 当前项目上添加类

javascript - 获取标题文本的值(HTML,CSS)

html - 仅将 css 样式设置为表格的第一行

Javascript 破坏 CSS 悬停

javascript - 当<video>时间发生变化时收到通知

javascript - 如果删除了父元素,则隐藏 Bootstrap Popover

javascript - append 到具有多个输入值的列表

JavaScript 使用字符串值作为对象的引用(不使用 eval())

javascript - Jquery使用html动态附加数据属性值

javascript - Slimscroll 在 IE7 中无法使用 height100%