Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.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 DOM解析器在img标记上给出错误_Javascript_Html_Domparser - Fatal编程技术网

Javascript DOM解析器在img标记上给出错误

Javascript DOM解析器在img标记上给出错误,javascript,html,domparser,Javascript,Html,Domparser,嗨,我正在尝试使用DOM解析器解析img,但由于某种原因,它抛出了解析错误。 请帮我找到解决办法。据我所知,图像标记不需要关闭标记,浏览器会在加载时删除它,这会造成解析问题 小提琴: <body> <img src="test"> </body> <body> <img src="test"/> </body> var html = document.body.innerHTML; function p

嗨,我正在尝试使用DOM解析器解析img,但由于某种原因,它抛出了解析错误。 请帮我找到解决办法。据我所知,图像标记不需要关闭标记,浏览器会在加载时删除它,这会造成解析问题

小提琴:

<body>
    <img src="test">
</body>
<body>
    <img src="test"/>
</body>
var html = document.body.innerHTML;
function parseXML(html)
{
    var parser, componentDoc;
    if (window.DOMParser)
    {
        parser = new DOMParser(); // should work for FF and IE 9
        componentDoc = parser.parseFromString(html,"application/xhtml+xml");//this line must be creating the issue. which mime type is better 
    }
    else
    {
        componentDoc = windowsParse(html);
    }
    return componentDoc;
} 

var component = parseXML(html);
console.log(component);

HTML:

<body>
    <img src="test">
</body>
<body>
    <img src="test"/>
</body>
var html = document.body.innerHTML;
function parseXML(html)
{
    var parser, componentDoc;
    if (window.DOMParser)
    {
        parser = new DOMParser(); // should work for FF and IE 9
        componentDoc = parser.parseFromString(html,"application/xhtml+xml");//this line must be creating the issue. which mime type is better 
    }
    else
    {
        componentDoc = windowsParse(html);
    }
    return componentDoc;
} 

var component = parseXML(html);
console.log(component);

在DOM解析器处理后,图像标记在内部被视为:

<img src="test">

请注意,它不是自动关闭(HTML中不存在),也没有关闭标记(因为
没有

(要演示这一点,请尝试
console.log(html);

因此,您试图解析的HTML是。。。嗯,只是一个开场白。但是您告诉它解析为XHTML+XML,它在
标记上没有这个规则


您可能会使用
text/html
作为文档类型,但是请记住,这将生成一个简单的
结构。

无效,使用
格式。即使这样也不起作用…请检查小提琴您可能需要在
html
标记之前使用类似
的东西来解析
应用程序/xhtml+xml