Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Internet Explorer中解析HTML(来自字符串)_Javascript_Html_Dom_Html Parsing - Fatal编程技术网

Javascript 在Internet Explorer中解析HTML(来自字符串)

Javascript 在Internet Explorer中解析HTML(来自字符串),javascript,html,dom,html-parsing,Javascript,Html,Dom,Html Parsing,我编写了以下跨浏览器代码,用于从字符串解析XML: var xparse = function (xml$s) { var xml, p; if (typeof(DOMParser) !== 'undefined') { var p = new DOMParser(); xml = p.parseFromString(xml$s, 'text/xml'); } else { xml = new ActiveXObject('

我编写了以下跨浏览器代码,用于从字符串解析XML:

var xparse = function (xml$s) {
    var xml, p;
    if (typeof(DOMParser) !== 'undefined') {
        var p = new DOMParser();
        xml = p.parseFromString(xml$s, 'text/xml');
    } else {
        xml = new ActiveXObject('Microsoft.XMLDOM');
        xml.async = 'false';
        xml.loadXML(xml$s);
    }

    return xml;
};

它可以很好地用于XML,但我一直在尝试将其用于解析HTML。对于除IE以外的任何浏览器,只需将
text/xml
更改为
text/html
。但是,我似乎找不到任何关于
ActiveXObject('Microsoft.XMLDOM')
的HTML等价物的信息。我还希望避免使用任何框架。

要将HTML字符串注入DOM元素,可以使用
innerHTML

element.innerHTML = xml$s;

但是你可能想这样做。

目的是什么?将其注入DOM?为此,您可以在元素上使用innerHTML,比如element.innerHTML=xml$s;我建议您加载jQuery并执行
$.get(文件名,函数(data(){这里的数据包含您的html});
@asgoth是的,虽然我不太喜欢innerHTML,但jQuery似乎就是这样处理的。