2010-08-11 54 views
0

我正在做一个应用程序,它必须将XML树中的元素解析为HTML,并且在所有浏览器中它都很顺利,但IE不希望将这些样式应用于元素。我怎么能解决这个问题?如何将类或样式应用于IE中的某个HTML元素?

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; 
} 
+1

你能告诉你如何做到这一点? – Jarek 2010-08-11 19:15:51

回答

0

除了在你的样式表中的相应的CSS类,你需要这样做:

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

请忽略'setAttribute'版本,在这种情况下没用。 IE不理解它,其他人不需要它。 – 2010-08-11 19:55:58

+0

哦?我认为这对所有其他浏览器都是必需的。谢谢,正式注意。 – montrealist 2010-08-11 19:56:46

+0

感谢它的作品!!!!!!,并为风格我怎么能做到这一点? – hidura 2010-08-11 20:02:00

相关问题