如何在网站上制作用户编写的HTML,而不是与网站的样式表冲突?

[英]How to make HTML written by users on a site, not conflict with the site's stylesheets?


I have a website that allows a user to create blog posts. There are some backlisted tags but most standard HTML tags are acceptable.

我有一个允许用户创建博客帖子的网站。有一些回溯标签,但大多数标准HTML标签都可以接受。

However, I'm having issues with how the pages get displayed.

但是,我遇到了如何显示页面的问题。

I keep the HTML wrapped in its own div.

我将HTML保存在自己的div中。

I would ultimately like to keep the HTML from the user separate from the main sites stylesheets so it can avoid inheriting styles and screwing up the layout of the originating site where the HTML is being displayed.

我最终希望将用户的HTML与主站点样式表分开,这样可以避免继承样式并搞砸显示HTML的原始站点的布局。

So in the end, is there anything I can apply to a div so its contents are quarantined from the rest of the site?

所以最后,有什么我可以应用于div,所以它的内容被隔离到网站的其余部分?

Thanks!

4 个解决方案

#1


You could use a reset stylesheet to reset the properties for that specific DIV and it’s children. And on the other side, you’ll probably need a CSS parser to adjust the user’s stylesheet for that specific DIV.

您可以使用重置样式表来重置特定DIV及其子项的属性。而另一方面,您可能需要一个CSS解析器来调整该特定DIV的用户样式表。

#2


You can do it in a frame or an iframe. That will keep it separate in every way.

您可以在框架或iframe中执行此操作。这将使它在各方面都分开。

#3


Could you format each user-generated content with a div of class 'username' in addition to any other classnames you may add automatically?

除了可以自动添加的任何其他类名之外,您是否可以使用div'class'div来格式化每个用户生成的内容?

Then they -I assume 'they'- can format and style as they please, can have all their styles prefaced like so: div.username selector.

然后他们 - 我认为'他们'可以随心所欲地格式化和设计样式,所有样式都可以这样开头:div.username selector。

Otherwise, you may be able to use iframes.

否则,您可以使用iframe。

#4


You could use an iframe to keep them completely separate if you really wanted to be extreme about it.

你可以使用iframe让它们完全分开,如果你真的想对它采取极端的话。

Or, you could restrict them to only writing inline styles so that they can't affect your page's stylesheet:

或者,您可以将它们限制为仅编写内联样式,以便它们不会影响页面的样式表:

1) strip out any style tags html the user creates. This way they can't override your styles.

1)去掉用户创建的任何样式标签html。这样他们就无法覆盖你的风格。

2) validate their code and either fix or reject things like unclosed tags or elements that shouldn't be inside a div (like head or body tags) and make sure it all gets closed properly so it can't mess up any html from your page after the div it's contained in.

2)验证他们的代码并修复或拒绝诸如未封闭的标签或不应该在div内的元素(如头部或身体标签)之类的东西,并确保它们都正确关闭,这样它就不会弄乱你的任何html它包含在div之后的页面。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/07/15/2bafbfe819d35179517660592fc4f5c6.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号