jQuery(几乎)相当于PHP的strip_tags() [英] jQuery (almost) equivalent of PHP's strip_tags()

查看:92
本文介绍了jQuery(几乎)相当于PHP的strip_tags()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有这个函数的jQuery版本?


$ b


string strip_tags (string $ str [,$

将字符串中的所有标签和内容从字符串中去除,除了在允许标签中定义的字符串。
$ b

像:

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

from:

 < div id =text> 
< p>段落< / p>
< div>应该被剥离< / div>
< / div>


解决方案

删除只是标签,而不是,这是PHP的 strip_tags()的行为,您可以这样做:

  var whitelist =p; //对于更多标签使用多重选择器,例如p,img
$(#text *)。not(whitelist).each(function(){
var content = $(this).contents();
$ (this).replaceWith(content);
});

在这里试试。


Is there a jQuery version of this function?

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

strip all tags and content inside them from a string except the ones defined in the allowable tags string.

like:

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

from:

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

解决方案

To remove just the tags, and not the content, which is how PHP's strip_tags() behaves, you can do:

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.

这篇关于jQuery(几乎)相当于PHP的strip_tags()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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