使用InnerHTML解析IMG标签Javascript [英] Parsing IMG tags Javascript using InnerHTML

查看:75
本文介绍了使用InnerHTML解析IMG标签Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是两难境地:

我正在执行JavaScript效果.为此,我使用.innerHTML拉节点及其子节点(包括图像).然后尝试通过DOM解析它.当到达图像标签时,它将引发解析错误.当我提醒innerHTML时,我看到它正在剥离IMG标签的结尾.

I am doing a javascript effect. To do so I am pulling a node and it's children (including images) with .innerHTML. Then trying to parse it via the DOM. When it gets to the image tags it throws a parse error. When I alert the innerHTML I see that it is stripping the closing for the IMG tags.

我不确定问题出在解析器还是innerHTML.如何获取该节点,获取内部内容并将其解析为XML?

I am not sure if the problem is with the parser or innerHTML. How can I take this node, grab the internal contents, parse it as XML?

这里发生了类似的情况: innerHTML从图像标记中删除了斜杠

Looks like a similar thing happened here: innerHTML removing closing slash from image tag

(这是我在互联网上搜索了将近两天后发现的唯一涉及此问题的页面.)

(This is the only page in the internet that I found that touches on this issue after almost two Days of searching.)

这是我正在使用的解析代码:

Here is the parse code I am using:

function loadXMLString(txt) {
    if (window.DOMParser) {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(txt,"text/xml");
    } else { // Internet Explorer
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(txt); 
    }
    return xmlDoc;
}

解决方案是更改mime类型,但是如何使用javascript解析器(MS ActiveX和其他浏览器的标准)来做到这一点?我应该使用什么哑剧?

The resolution was to change the mime type, but how do you do that with the javascript parser (both MS ActiveX and the other browser's standard)? What mime should I use?

这是我尝试解析的DOM元素:

Here is the DOM Element I am attempting to parse:

<div style="display:none" id="ItemsContainer" name="ItemsContainer">
    <SPAN>
       <a href="url1"><img src="1.jpg" alt="alt1" /></a>
       <a href="url2"><img src="2.jpg" alt="alt2" /></a>
       <a href="url3"><img src="3.png" alt="alt3" /></a>
       <a href="url4"><img src="4.jpg" alt="alt4" /></a>
    </SPAN>
</div>

如果我将标签更改为另一个名称,则可以正常工作.似乎innerHTML正在破坏标签,或者解析器无法解析IMG标签.

If I change the tags to another name, like then it works. It seems that innerHTML is breaking the tag or that the parser can't parse IMG tags.

请告知.在此先感谢!

推荐答案

IE自动将标签名称大写(因此成为),因此我使用了 txt.replace(/>< \/a>/g,"/></a>).replace(//< \/A>/g,"/></A>)

IE automatically capitalizes Tag names (so becomes ) so I used txt.replace(/><\/a>/g, " /></a>").replace(/><\/A>/g, " /></A>")

感谢所有提供帮助的人!

Thanks to all who helped!

这篇关于使用InnerHTML解析IMG标签Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆