清除特定div的所有页面样式,它是子元素 [英] Clear all page style for a specific div and it's child elements

查看:125
本文介绍了清除特定div的所有页面样式,它是子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些html嵌入在网站上,但被嵌入在CSS的页面搞乱了。我如何使它不会发生...我的内容有一个特定的ID,它的CSS是非常具体的。它包含图像,divs,跨度,h标签,p标签..所有通常的东西。

I have some html being embeded on websites but is gets messed by the page it's embeded in's css. How can I make it so that doesnt happen... My content has a specific ID and it's css is very specific. It contains images, divs, spans, h tags, p tags.. all the usual stuff. The style sheet for MY div is appended to the head.

任何想法?

推荐答案

如果我正确理解您的问题,您可以递归遍历<您的 div 的一个href =http://www.w3schools.com/htmldom/dom_nodetree.asp =nofollow>子元素并重置 和< a href =http://www.w3schools.com/tags/att_standard_style.asp =nofollow> style 属性。请参阅小提示或以下代码段:

Provided I understood your question correctly, you could recursively iterate through the child elements of your div with JavaScript and reset the class and style attributes. See this fiddle or the following code snippet:

<html>
    <head>
        <style type="text/css">
            .aClass {
                font-size: 0.75em;
            }

            .anotherClass {
                font-weight: bold;
                color: Fuchsia;
            }
        </style>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            function removeStuff(elem) {
                elem.children().each(function() {
                    if ($(this).children().length > 0) {
                        $(this).removeClass();
                        $(this).removeAttr('style');
                        removeStuff($(this));
                    } else {
                        $(this).removeClass();
                        $(this).removeAttr('style');
                    }
                });
            }
        </script>
    </head>
    <body>
        <div id="myDiv">
            <span class="aClass">This span is classy.</span>
            <div style="font-size: 2em;">
                This div has style.
                <span class="anotherClass">Lorem Ipsum.</span>
            </div>
        </div>
        <a href="javascript:removeStuff($('#myDiv'));">Remove classes &amp; styles</a>
    </body>
</html>

这篇关于清除特定div的所有页面样式,它是子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆