【发布时间】:2013-01-30 22:23:29
【问题描述】:
功能:
function disable_hidden_flip_questions(option, value){
var first_q = document.getElementById('hidden_auto_questions');
var second_q = document.getElementById('hidden_include_auto');
var inputs_list = [];
if(option == 'all'){
inputs_list.push(first_q.getElementsByTagName("input"));
inputs_list.push(second_q.getElementsByTagName("input"));
}else if (option == 'first'){
inputs_list.push(first_q.getElementsByTagName("input"));
}else if(option == 'second'){
inputs_list.push(second_q.getElementsByTagName("input"));
}
for (var inputs in inputs_list){
for(var input in inputs_list[inputs]){
if(inputs_list[inputs].hasOwnProperty(input) && input != 'length'){
if(!value){
inputs_list[inputs][input].removeAttribute('disabled');
}else{
inputs_list[inputs][input].setAttribute("disabled", "disabled");
}
}
}
}
}
在 Chrome 和 Firefox 中运行良好,但在 IE9 中,控制台在这一行给我一个错误:
inputs_list[inputs][input].setAttribute("disabled", "disabled")
如果我 console.log(inputs_list[inputs][input]) 我在 Chrome 中得到这个:
(输入是由 RoR 生成的,这就是为什么它的 name 和 id 这么长)
<input class="radio_buttons optional" id="custom_attributes_trailer_insurance_endorsement_false" name="custom_attributes[trailer_insurance_endorsement]" type="radio" value="false" disabled="disabled">
是的,这就是我想要的(和期望的)...但是在 IE9 的控制台中我得到了这个:
[object HTMLCollection]
这完全没用……
所以问题:在 IE 的崇高观点中我哪里出错了?我知道 IE9 支持 setAttribute,所以我假设它与我的 for...in 循环有关。
编辑:因此,由于 cmets,问题可能出在对象类型上(HTMLCollection 存储在 Array 中)。如果确实如此,我该如何让所有数据类型都兼容?
【问题讨论】:
-
有什么理由不使用 jQuery 来避免这种类型的跨浏览器垃圾?
-
是的 - jQuery 中有很多不错的东西和库,但是 this 是使用它的真正原因(或类似的跨浏览器解决方案)。
-
@StephenP 你有 jquery 解决方案吗?
-
@ryan - 正如@vaporbook 在他的回答中指出的那样,您从
getElementsByTagName中得到一个HTMLCollection- 浏览器实现为您提供的“本机”对象,因此可以因浏览器而异(虽然它不应该)——使用像 jQuery 这样的库的主要原因是 it 处理像这样的跨浏览器差异。 jQuery 还为您提供了更一致的处理方式。在 jQuery 中,您可能会按照$("#hidden_auto_questions input").each(call_my_function);的方式做一些事情(注意 CSS 样式选择器)
标签: javascript internet-explorer-9