简体   繁体   English

如何删除所有常规html标签除外 <a></a> , <img> (里面的属性)和 <br> 用javascript?

[英]How can I Strip all regular html tags except <a></a>, <img>(attributes inside) and <br> with javascript?

When a user create a message there is a multibox and this multibox is connected to a design panel which lets users change fonts, color, size etc.. When the message is submited the message will be displayed with html tags if the user have changed color, size etc on the font. 当用户创建消息时,有一个多箱,这个多箱连接到一个设计面板,让用户可以更改字体,颜色,大小等。当提交消息时,如果用户改变了颜色,消息将显示为带有html标签的消息,字体大小等。

Note: I need the design panel, I know its possible to remove it but this is not the case :) 注意:我需要设计面板,我知道可以删除它,但事实并非如此:)

It's a Sharepoint standard, The only solution I have is to use javascript to strip these tags when it displayed. 这是一个Sharepoint标准,我唯一的解决方案是使用javascript在显示时剥离这些标记。 The user should only be able to insert links, images and add linebreaks. 用户应该只能插入链接,图像和添加换行符。

Which means that all html tags should be stripped except <a></a> , <img> and <br> tags. 这意味着除了<a></a><img><br>标签之外,所有html标签都应该被剥离。

Its also important that the attributes inside the the <img> tag that wont be removed. 同样重要的是<img>标签内的属性不会被删除。 It could be isplayed like this: 它可以像这样显示:

<img src="/image/Penguins.jpg" alt="Penguins.jpg" style="margin:5px;width:331px;">

How can I accomplish this with javascript? 我怎样才能通过javascript实现这一目标?

I used to use this following codebehind C# code which worked perfectly but it would strip all html tags except <br> tag only. 我用它完美地工作这个隐藏代码如下C#代码,但它会剥夺除了所有的HTML标签<br>仅标签。

public string Strip(string text)
{
   return Regex.Replace(text, @"<(?!br[\x20/>])[^<>]+>", string.Empty);
}

Any kind of help is appreciated alot 任何形式的帮助都很受欢迎

Does this do what you want? 这样做你想要的吗? http://jsfiddle.net/smerny/r7vhd/ http://jsfiddle.net/smerny/r7vhd/

$("body").find("*").not("a,img,br").each(function() {
    $(this).replaceWith(this.innerHTML);
});

Basically select everything except a, img, br and replace them with their content. 基本上选择除a,img,br之外的所有内容,并用它们的内容替换它们。

Smerny's answer is working well except that the HTML structure is like: Smerny的答案很有效,只是HTML结构如下:

var s = '<div><div><a href="link">Link</a><span> Span</span><li></li></div></div>';
var $s = $(s);
$s.find("*").not("a,img,br").each(function() {
    $(this).replaceWith(this.innerHTML);
});
console.log($s.html());

The live code is here: http://jsfiddle.net/btvuut55/1/ 实时代码在这里: http//jsfiddle.net/btvuut55/1/

This happens when there are more than two wrapper outside (two div s in the example above). 当外部有两个以上的包装器时(上例中的两个div ),会发生这种情况。

Because jQuery reaches the most outside div first, and its innerHTML , which contains span has been retained. 因为jQuery首先到达最外面的div ,并且其包含span innerHTML已被保留。

This answer $('#container').find('*:not(br,a,img)').contents().unwrap() fails to deal with tags with empty content. 这回答$('#container').find('*:not(br,a,img)').contents().unwrap()无法处理带有空内容的标签。

A working solution is simple: loop from the most inner element towards outside : 一个可行的解决方案很简单: 从最内部元素向外部循环

var $elements = $s.find("*").not("a,img,br");
for (var i = $elements.length - 1; i >= 0; i--) {
    var e = $elements[i];
    $(e).replaceWith(e.innerHTML);
}

The working copy is: http://jsfiddle.net/btvuut55/3/ 工作副本是: http//jsfiddle.net/btvuut55/3/

with jQuery you can find all the elements you don't want - then use unwrap to strip the tags 使用jQuery,你可以找到你不想要的所有元素 - 然后使用unwrap来剥离标签

$('#container').find('*:not(br,a,img)').contents().unwrap()

FIDDLE 小提琴

I think it would be better to extract to good tags. 我认为提取好标签会更好。 It is easy to match a few tags than to remove the rest of the element and all html possibilities. 很容易匹配几个标签,而不是删除元素的其余部分和所有html的可能性。 Try something like this, I tested it and it works fine: 尝试这样的东西,我测试它,它工作正常:

// the following regex matches the good tags with attrinutes an inner content
var ptt = new  RegExp("<(?:img|a|br){1}.*/?>(?:(?:.|\n)*</(?:img|a|br){1}>)?", "g");
var input = "<this string would contain the html input to clean>";              
var result = "";

var match = ptt.exec(input);                
while (match) {
    result += match;
    match = ptt.exec(input);
}

// result will contain the clean HTML with only the good tags
console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM