IE 上的 JavaScript DOM
我想做一个切换可见/隐藏的JS函数。
var foo = function(n){
var hidden_elements = document.getElementsByName('hidden');
for(var i=0;i<hidden_elements.length;i++){
hidden_elements[i].style.visibility = 'hidden';
}
hidden_elements[n].style.visibility = 'visible';
};
它适用于 Firefox 和 Chrome,但不适用于 IE。为什么? 提前致谢。
I want to make a JS function that switch visible/hidden.
var foo = function(n){
var hidden_elements = document.getElementsByName('hidden');
for(var i=0;i<hidden_elements.length;i++){
hidden_elements[i].style.visibility = 'hidden';
}
hidden_elements[n].style.visibility = 'visible';
};
It works on Firefox and Chrome, but it doesn't on IE. Why?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您避免恐惧并使用:
这些库做了很多工作来消除不同浏览器的意外情况。如果您是超级极简主义者,您可以随时查看源代码他们如何处理差异。另请查看quirksmode 的兼容性列表。
我知道我没有给出可靠的答案,但你总会遇到这些麻烦,而这些是解决这些问题的一些好工具。
I would recommend saving yourself the horror and going with:
The libraries do a lot to smooth over the surprises of different browsers. If you are being super minimalist you can always check the source for how they are handling the differences. Also have a look at quirksmode's compatibility listing.
I know I didn't give a solid answer but you are going to run into these troubles all the time and these are some good tools for hammering them out.
IE 至 IE8 不遵循 W3C 规范。微软有自己的标准。许多适用于 Firefox 或 Chrome(W3C 标准)的脚本方法可能无法在 IE 的各种版本中正常工作。
为什么不从头开始尝试一些事情呢?要么那样,要么做一些方便访问的事情。您可以通过为 ids 创建模式并动态构建这些 ids(可能是增量的)来做到这一点。然后,从标签的 ID 访问这些标签。
不推荐通过名称访问。 id最合适。
IE up to IE8 does not follow W3C specs. Microsoft has their own standards. Many scripting methods that work on Firefox or Chrome (which are W3C standards) may not work properly in various builds on IE.
Why don't you try something from scratch? Either that, or do some ease of access. You can do this by making a pattern for ids and dynamically building those ids (may be incremental). Then, access those tags from their id.
Access by name is not preferred. Id is most appropriate.
您的 html 无效。
"name"
属性必须是唯一的。请改用"class"
。Internet Explorer 可能会出现一些问题,因此可以使用像 flowjs 这样的 DOM 填充。
Your html is invalid. the
"name"
property needs to be unique. Use"class"
instead.Internet Explorer might give some issues, so DOM polyfills like flowjs could be used.