javascript 替换允许的 html 标签
我需要使用 javascript 删除所有 html 标签,除了我明确允许的标签。我有一个表单,只允许使用以下标签及其各自的结束标签:
<b> <strong> <i> <em> <u> <br> <pre>
<blockquote> <ol> <ul> <li>
<a href="http://www.somesite.com">link</a>
应删除所有其他标记。我一直在搜索,但只找到删除所有标签或删除单个标签的实例。这可以简单地完成吗?我不能使用 PHP,必须使用 javascript。有什么解决办法吗?
谢谢!
I need to use javascript to remove all html tags, except for those I explicitly allow. I have a form that only allows the following tags and their respective end tags:
<b> <strong> <i> <em> <u> <br> <pre>
<blockquote> <ol> <ul> <li>
<a href="http://www.somesite.com">link</a>
All other markup should be removed. I have been searching but I have only found instances where all tags are removed or a single tag is removed. Can this be done simply? I cannot use PHP, must be javascript. Any solutions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保容器不高于
body
标记。或者,当它取出head
、html
、script
等标签时,您可能会遇到问题。另外,如果您想要 :not 可以是一个列表,您可以:
或者甚至将其放入 removeTags 函数中。 (可能性是无穷无尽的......)
编辑:正如一些人在评论中指出的那样:Javascript可以被关闭。另一件事是我假设你想保留所有内部信息。如果没有,那么就像 megakorre 所建议的那样,只需删除()就足够了。
Make sure the container is nothing higher than the
body
tag. Or you might have issues when it takes outhead
,html
,script
etc. tags.Also if you want the :not could be a list and you could:
Or even put this in the removeTags function. (the possibilities are endless ... )
EDIT: as some have noted in the comments: Javascript can be turned off. The other thing is I assumed you wanted to keep all the inner information. If not then just a remove() will suffice as megakorre suggests.
所以享受一切形式的安全
女巫我猜你首先要做的就是
你可以将内容放在 div 中并调用
so sipping all form of security
witch im gessing is why you are doing it in the first place
you can put the content in a div and call
正如评论中指出的,这不能安全地完成。绕过 javascript 过滤器将非常容易。这必须在服务器端实现。
As pointed out in the comments, this can't be done securely. It will be very easy to to circumvent the javascript filter. This must be implemented server side.