简体   繁体   English

如何将类或样式应用于IE中的某些HTML元素?

[英]How could I apply the class or the style to some HTML element in IE?

I'm doing an application that has to parse elements in an XML tree to HTML and in all the browsers it goes well but IE doesn't want to apply the styles to the elements. 我正在做一个应用程序,它必须将XML树中的元素解析为HTML,并且在所有浏览器中都运行良好,但是IE不想将样式应用于元素。 How could I fix this? 我该如何解决?

function add2Doc(elmnt, domDoc, newEl)
{
    /*
     * This function transform an 
     * element to into the an acceptable
     * part of another document, I know
     * exist some beatiful function called
     * importNode but IE, like always doesn't
     * accept.
     */
    if (typeof newEl == 'undefined'){ 
        var newEl = domDoc.createElement(elmnt.tagName);
    }
    if (elmnt.attributes.length > 0){
        for (var cont = 0; cont < elmnt.attributes.length; cont++){
            if (getBrowser() == 0){
                /*
                 * This part of the code it's for the
                 * assholes of MS, what doesn't have 
                 * any kind of consideration for 
                 * the others, this kind of things
                 * consume resources and 
                 * makes more slower the crap of IE.
                 */
                if (elmnt.attributes[cont].specified == true){
                    newEl.setAttribute(elmnt.attributes[cont].nodeName, elmnt.attributes[cont].nodeValue);
                }
            }else{
                    newEl.setAttribute(elmnt.attributes[cont].nodeName, elmnt.attributes[cont].nodeValue);
            }
        }
    }
    childs = elmnt.childNodes;
    if (elmnt.childNodes.length > 0){
        for (var cont = 0; cont < elmnt.childNodes.length; cont++){
            child = elmnt.childNodes[cont];
            if (child.nodeType == 1){
                newChild = new add2Doc(child, domDoc, domDoc.createElement(child.tagName));
                newEl.appendChild(newChild);
            }else if(child.nodeType == 3){
                if (getBrowser() == 1){
                    newEl.appendChild(domDoc.createTextNode(child.nodeValue));
                }else{
                        if (newEl.tagName == 'STYLE'){
                            newEl.csstext = child.nodeValue;
                        }else if (newEl.tagName == 'SCRIPT'){
                            newEl.text = child.nodeValue;
                        } else{
                        newEl.innerText = child.nodeValue;
                        }
                    }
            }

        }
    }
    return newEl;
}

You can either apply browser specific stylesheets or put inline css hacks to fix these things. 您可以应用特定于浏览器的样式表,也可以放置内联CSS hack来解决这些问题。

http://www.maratz.com/blog/archives/2005/06/16/essentials-of-css-hacking-for-internet-explorer/ http://www.maratz.com/blog/archives/2005/06/16/essentials-of-css-hacking-for-internet-explorer/

In addition to having the appropriate CSS class in your stylesheet, you need to do this: 除了在样式表中包含适当的CSS类之外,您还需要执行以下操作:

var box = document.createElement("div"); 
box.className = 'your_css_class'; /*forces styling in ie*/

You should use conditional comments : 您应该使用条件注释:

<!--[if IE]>
<script type="text/javascript">
// do something here
</script>
<![endif]-->

Article here : http://www.quirksmode.org/css/condcom.html 文章在这里: http : //www.quirksmode.org/css/condcom.html

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

相关问题 如何检查html元素是否在IE6-IE8的样式标签中设置了某些样式属性? - How to check if html element has some style properties set in style tag in IE6 - IE8? 如何将风格应用于 <HTML> 元件? - How to apply a style to the <HTML> element? 将某些事件上的样式应用于具有id Accordian的html的每个元素 - Apply style on some event to each element of html with id accordian 如何将类应用于动态插入的html元素? - How to apply class to a dynamic inserted html element? 如何将样式应用于类的单个元素,同时从同一个类的前一个元素中删除样式? - How to apply style to a single element of a class, while removing style from the previous element of the same class? 使用 Javascript 或 CSS 将某些样式应用于特定元素 - Apply some style to specific element with Javascript or CSS 如何在jQuery中将样式动态应用于元素,类型和类? - How do you dynamically apply style to an element, type and class in jQuery? 如何使用JS将html元素放在一些绝对坐标中,而不管体型(位置,边距)如何? - How can I place an html element in some absolute coordinates regardless of the body style (position, margin) using JS? 如何将一些样式应用于 JavaScript 的“节点”对象 - How can I apply some style to JavaScript's "node" object 如何在 html 中隐藏样式元素 - How can I hide a style element in html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM