views:

42

answers:

3

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. 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;
}
A: 

You can either apply browser specific stylesheets or put inline css hacks to fix these things.

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

Jamie Wong
Jamie, thanks but thats div works perfectly when i put inside the HTML code orginal but when i've tried to append the div after the loaded the IE simply don't load the style it's very frutrating because in the other browsers the code runs perfectly.
hidura
A: 

In addition to having the appropriate CSS class in your stylesheet, you need to do this:

var box = document.createElement("div"); 
box.className = 'your_css_class'; /*forces styling in ie*/
dalbaeb
Please leave out the `setAttribute` version, it's useless in this case. IE doesn't understand it, the others don't need it.
Marcel Korpel
Oh? I thought this was necessary for all the other browsers. Thanks, duly noted.
dalbaeb
Thanks it works!!!!!!, and for the style how could i do it?
hidura
I divide the code one part for IE, and the other for everybody.
hidura
@hidura: No, don't! It's completely unnecessary in this case; like I said, solely use `element.className = '…';`.
Marcel Korpel
But the code not was created just for apply the class he takes one element in a XML tree and passes to the document or another XML tree like importNode what doesn't works in IE
hidura
For the style, you need to code it up in CSS. Something like: `.your_css_class { color: red; }`
dalbaeb
I know what going to do with IE. Thanks!!!!!!!!!!!!!!!
hidura
It's funny IE has always the ways to peace me off, with the tables there something special to do?
hidura
A: 

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

mathieu