我如何将我的代码转换为在chrome中工作

时间:2014-03-06 11:31:27

标签: javascript xml google-chrome

我有一个仅适用于IE而不是Chrome的功能,因为“Microsoft.XMLDOM”和selectSingleNode(我认为),请帮我转换代码到Chrome,谢谢!

var xmlDictionary = null;    

function 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 个答案:

答案 0 :(得分: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);
            }



            }