2014-03-06 57 views
-1

我有一個功能,只適用於IE不在Chrome,因爲「Microsoft.XMLDOM」和selectSingleNode(我認爲),請幫助我將代碼轉換爲Chrome,謝謝!我如何轉換我的代碼在鉻中工作

var xmlDictionary = null;  

功能onGridMembersSelection(ID,XML) {

var domDoc = new ActiveXObject("Microsoft.XMLDOM");     
    domDoc.loadXML(xml);  

       var helpHtml2 = ""; 
       var xmlhttp = new XMLHttpRequest(); 
       xmlhttp.open("GET", "Dictionary.xml", true);    
       xmlDictionary = xmlhttp.responseXML;     
       xmlhttp.send(); 

     helpHtml2 += xmlDictionary.selectSingleNode("Terms/Term[Key='" + domDoc.selectSingleNode("Members/Member/@UName").text + "']/Desc").text; 
     alert(helpHtml2); 

} 
+1

這似乎有用:http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp – endofsource

回答

0

可以使用xmlhttp=new XMLHttpRequest();代替: var domDoc = new ActiveXObject("Microsoft.XMLDOM");。 適用於其他瀏覽器(Firefox,Chrome,Opera,Safari)。

並加載XML文件,請使用:

parser=new DOMParser(); 
    xmlDoc=parser.parseFromString(txt,"text/xml"); 

如下鏈接心滿意足的..

http://www.w3schools.com/xml/xml_dom.asp

請試試這個以及..

   var xmlDictionary = null;  
      function onGridMembersSelection(id,xml) { 

      // code for IE 
      if (window.ActiveXObject || xhttp.responseType=="msxml-document") 
      { 
       var domDoc = new ActiveXObject("Microsoft.XMLDOM");     
       domDoc.loadXML(xml);  

          var helpHtml2 = ""; 
          var xmlhttp = new XMLHttpRequest(); 
          xmlhttp.open("GET", "Dictionary.xml", true);    
          xmlDictionary = xmlhttp.responseXML;     
          xmlhttp.send(); 

        helpHtml2 += xmlDictionary.selectSingleNode("Terms/Term[Key='" + domDoc.selectSingleNode("Members/Member/@UName").text + "']/Desc").text; 
        alert(helpHtml2); 
      } 
      // code for Chrome, Firefox, Opera, etc. 
      else if (document.implementation && document.implementation.createDocument) 
      { 
      var domDoc=new XMLHttpRequest(); 
      xmlhttp.open("GET", "Dictionary.xml", true);    
      xmlDictionary = xmlhttp.responseXML;    
       xmlhttp.send(); 
       xml.evaluate("Terms/Term[Key='" +xml.evaluate("Members/Member/@UName", xmlDictionary , null, XPathResult.ANY_TYPE, null), xmlDictionary , null, XPathResult.ANY_TYPE, null).nodeValue; 
       helpHtml2 += .text; 
       alert(helpHtml2); 
      } 



      } 
+0

謝謝,我試過,並且由於某種原因不起作用,也許問題出在xmlDictionary.selectSingleNode和domDoc.selectSingleNode上? – user3379151