2010-08-11 60 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

相關問題