jQuery (almost) equivalent of PHP's strip_tags()

后端 未结 9 843
忘掉有多难
忘掉有多难 2020-11-27 04:50

Is there a jQuery version of this function?

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

stri

9条回答
  •  不知归路
    2020-11-27 05:13

    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.

提交回复
热议问题