简体   繁体   English

如果未指定NAME属性(Javascript,HTML5和Internet Explorer),则getElementById在IE中不起作用

[英]getElementById does not work in IE if NAME attribute is not specified (Javascript, HTML5 and Internet Explorer)

I have this problem of an image rollover javascript not working in Internet Explorer if I don't specify the NAME attribute in the tag. 如果我没有在标记中指定NAME属性,就会遇到图像翻转JavaScript无法在Internet Explorer中运行的问题。 The other problem is when I do, the HTML5 validator will come up with a message saying "NAME attr is obsolete, use ID instead" which is what I am using with getElementById. 另一个问题是,当我这样做时,HTML5验证器将显示一条消息,指出“ NAME attr已过时,请改用ID”,这是我与getElementById一起使用的内容。

Should I specify a NAME attribute anyway and ignore the error messages or is there a workaround? 我是否仍应指定NAME属性并忽略错误消息,或者有解决方法? I don't wish to add extra attributes if I don't have too. 如果没有,我也不想添加额外的属性。

Thanks 谢谢

Adam 亚当

You aren't calling getElementById ; 您没有在调用getElementById ; you're writing document[img_name] . 您正在编写document[img_name]
Don't. 别。

Check to make sure you do not have duplicate ids. 检查以确保您没有重复的ID。 getElementById incorrectly gets name="" in certain IEs, if there is no element with the id. 如果没有带有ID的元素,则getElementById在某些IE中会错误地获取name=""

Also, make sure the document is not in quirks mode and is in standards mode. 另外,请确保文档不是处于怪异模式,而是处于标准模式。 alert( document.compatMode ) and make sure it isnt BackCompat. alert( document.compatMode )并确保它不是BackCompat。 If it is, use a proper doctype and kill comments/whitespace before doctype. 如果是这样,请使用适当的doctype,并在doctype之前取消注释/空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript在Internet Explorer 8中不起作用 - Javascript does not work in Internet Explorer 8 HTML5视频:隐藏控件在Internet Explorer,removeAttr,removeProp等中不起作用 - HTML5 Video: Hiding controls does not work in internet explorer, removeAttr, removeProp, etc HTML-Searchbar在Internet Explorer中不起作用 - HTML - Searchbar does not work in internet Explorer 为什么此Javascript可在Firefox中运行,但不能在Internet Explorer中运行? - Why does this Javascript work in Firefox, but not Internet Explorer? HTML5 <audio> 标记在Firefox和Internet Explorer上不起作用 - HTML5 <audio> tags donesn't work on Firefox and Internet Explorer 获取getElementByID以使用html5 - getting getElementByID to work html5 HTML5如何为javascript游戏替换Internet Explorer的WebAudio API? - HTML5 How to replace WebAudio API for Internet Explorer for javascript games? 全屏HTML5中的javascript打开(页面元素)在Internet Explorer中失败 - javascript open (page element) in fullscreen HTML5 fails in Internet Explorer JavaScript:Internet Explorer中document.getElementById()出现错误 - JavaScript: Error at document.getElementById() in Internet Explorer 使srcset属性在残破的HTML标记中可用于Internet Explorer - Make srcset attribute work for Internet Explorer in broken HTML tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM