本文介绍了我如何去除< a>< / a>,< img>(内部属性)和< br>与JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户创建消息时,会出现一个多框,并且此多框连接到允许用户更改字体,颜色,大小等的设计面板。当消息被提交时,如果用户使用HTML标签已经改变了字体的颜色,大小等。



注意:我需要设计面板,我知道可以将其删除,但情况并非如此: p>

这是一个Sharepoint标准,我唯一的解决方案是使用javascript在显示时去掉这些标签。用户应该只能插入链接,图片和添加换行符。



这意味着除< a> < / code>,< img> < br> 标签。

同样重要的是,< img> 标签中的属性不会被删除。它可能是这样显示:

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

我怎样才能用javascript做到这一点?



我曾经使用下面的代码隐藏C#代码,这些代码完美地工作,但它会去除除< br> 标签之外的所有html标签。

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

任何形式的帮助都会被赞赏很多

解决方案

这是做你想做的吗?



<$ p $($)$ $ $(this).replaceWith(body)。find(*)。不是(a,img,br)。 (this.innerHTML);
});

基本上选择除a,img,br之外的所有内容,并用它们的内容替换它们。


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.

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. 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.

Its also important that the attributes inside the the <img> tag that wont be removed. 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?

I used to use this following codebehind C# code which worked perfectly but it would strip all html tags except <br> tag only.

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/

$("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.

这篇关于我如何去除&lt; a&gt;&lt; / a&gt;,&lt; img&gt;(内部属性)和&lt; br&gt;与JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 09:56