Internet Explorer innerHTML输出没有引号的属性 [英] Internet Explorer innerHTML outputs attributes without quotes

查看:124
本文介绍了Internet Explorer innerHTML输出没有引号的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IE 8并尝试设置元素Id属性。然后我将该元素附加到父元素并检查它的innerHTML。我看到的问题是id属性缺少双引号。起初我认为这可能是因为我使用的是setAttribute属性,并且可能在IE 8中有问题,所以我使用jQuery来设置属性值,但问题仍然存在。继承我的代码...

I am using IE 8 and trying to set an an elements Id attribute. I then append that element to a parent element and check it's innerHTML. The problem I see is that the id attribute is missing double quotes. At first I thought this might be cause i was using setAttribute property and that might be buggy in IE 8, so i used jQuery to set attribute values but the problem was still there. Heres my code...

            var imgId="my" + val_imgarea + "img";
            var img=document.createElement('img');
            $(img).attr("Id",imgId);
            img.setAttribute('src',name);
            img.setAttribute('style',"width:100%;height:100%");
            $(img).click(function(){clic(imgId);});

            var input=document.createElement('input');
            input.setAttribute('type','text');
            input.setAttribute('id',name);
            input.setAttribute('style',"display:none");

            var parent=document.getElementById(newArea);
            parent.appendChild(img);
            parent.appendChild(input);

            alert($("#"+newArea).html());

输出

<IMG id=my21img style="width:100%;height:100%" src="a/img.jpg" />

我需要带引号的双引号然后我将此html写入文件然后其他一些应用程序读取它并导致问题引发缺失的引用。

I need the double quotes with the id cauze I then write this html to a file and then some other applications read it and its causing problems cauze of the missing quotations.

推荐答案

HTML不需要用引号包装属性, IE 8和更低版本返回没有引号的属性,除非它们中有空格。解析此HTML的其他应用程序显然不是HTML解析器,如果它们是预期的,则需要修复它们。

HTML doesn't require attributes to be wrapped with quotes, IE 8 and lower returns attributes without quotes unless they have white space in them. The other applications parsing this HTML are clearly not HTML parsers and, if they are intended to be, the fault needs fixing with them.


'在某些情况下,作者可以指定没有任何引号的属性值。属性值可能只包含字母(a-z和A-Z),数字(0-9),连字符(ASCII十进制45),句点(ASCII十进制46),下划线(ASCII十进制95)和冒号(ASCII十进制58)。我们建议使用引号,即使可以消除它们。'

  • http://www.w3.org/TR/html4/intro/sgmltut.html#attributes

请注意,在输出中,您有一个结束斜杠 / < IMG 标记的末尾。 Internet Explorer也没有将它们放入其中(我认为任何浏览器都没有)。这是一个XML习语,也不是HTML所必需的。

Note that, in your output, you have a closing slash / at the end of the <IMG tag. Internet Explorer doesn't put those in either (I don't think any browser does). This is an XML idiom and also not required for HTML.

这篇关于Internet Explorer innerHTML输出没有引号的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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