尝试使用jquery从元素中删除所有html

时间:2022-08-27 12:32:59

I have a DIV element that's filled with a bunch of HTML entities that I want to remove:

我有一个DIV元素,里面填充了一堆我要删除的HTML实体:

<div class="text">
<p>&lt;p&gt;&#8203;&lt;span style="line-height:25px;"&gt;&#8203;&lt;/span&gt;&lt;span style="line-height:25px;"&gt;&#8203;Hej hop!&lt;/span&gt;&nbsp;&lt;span style="line-height:25px;"&gt;&#8203;&lt;/span&gt;&lt;span</p>
</div>

I'm using jQuery and created this code but it isn't working:

我正在使用jQuery并创建此代码,但它无法正常工作:

$('.text').each(function () {
    $(this).replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
});

I'm trying to replace the text with the phrase "Hej Hop!".

我正在尝试用短语“Hej Hop!”替换文本。

The regex is not wrong..

正则表达式没错。

Here is how I did it with some other pages:

以下是我使用其他一些页面的方法:

    text = text.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");

But this was in a javascript function with parameters that returned the text.. But on this specific page I need to use jquery and iterate and check...

但这是一个javascript函数,其参数返回文本..但在这个特定的页面上我需要使用jquery并迭代并检查...

Question Solved:

$('text').each(function () {
    $(this).html(function (i, v) {
        return $('<div>').html(v).text();
    });
});

3 个解决方案

#1


2  

You can strip them like this using .html()

您可以使用.html()将它们剥离

$('div.text').html(function(i,v){
    return $('<div>').html(v).text();
    // create new div - append HTML and get the text from the div
});

http://jsfiddle.net/UA9P9/

It still leaves some characters in there so you will probably need to regex them out with your regex

它仍然留下一些字符,所以你可能需要用你的正则表达式将它们重新出现

$('div.text').html(function(i,v){
     return $.trim($('<div>').html(v).text().replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, ""));
});

http://jsfiddle.net/VfxaT/

#2


7  

You just need:

您只需:

$('.text').empty();

edit — if you want to remove the markup from the contents of the element, the simplest thing to do would be what Marc B suggested in a comment:

编辑 - 如果你想从元素的内容中删除标记,最简单的事情就是Marc B在评论中建议:

$('.text').each(function() {
  $(this).text($(this).text());
});

#3


0  

These are Ascii code so the Regex for that is: /xxx[\x00-\x7F]+xxx/

这些是Ascii代码,因此正则表达式为:/ xxx [\ x00- \ x7F] + xxx /

#1


2  

You can strip them like this using .html()

您可以使用.html()将它们剥离

$('div.text').html(function(i,v){
    return $('<div>').html(v).text();
    // create new div - append HTML and get the text from the div
});

http://jsfiddle.net/UA9P9/

It still leaves some characters in there so you will probably need to regex them out with your regex

它仍然留下一些字符,所以你可能需要用你的正则表达式将它们重新出现

$('div.text').html(function(i,v){
     return $.trim($('<div>').html(v).text().replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, ""));
});

http://jsfiddle.net/VfxaT/

#2


7  

You just need:

您只需:

$('.text').empty();

edit — if you want to remove the markup from the contents of the element, the simplest thing to do would be what Marc B suggested in a comment:

编辑 - 如果你想从元素的内容中删除标记,最简单的事情就是Marc B在评论中建议:

$('.text').each(function() {
  $(this).text($(this).text());
});

#3


0  

These are Ascii code so the Regex for that is: /xxx[\x00-\x7F]+xxx/

这些是Ascii代码,因此正则表达式为:/ xxx [\ x00- \ x7F] + xxx /