IE9 错误:对象不支持属性或方法“setAttribute”
IE9 error: Object doesn't support property or method 'setAttribute'
Function:
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
循环有关。
编辑:所以由于注释,问题可能出在对象类型(HTMLCollection
存储在Array
中)。如果确实如此,我该如何使所有数据类型兼容?
HTMLCollections 并不是真正的数组,因此以这种方式处理它们可能会得到不一致的结果。虽然所有浏览器都会从getElementsByTagName返回HTMLCollection,但它们在允许您如何处理它们方面可能存在重大差异。最好的方法是使用 DOM 方法从 HTMLCollection 访问内容 - 因此您将使用 .item(0) 来确保引用的是 dom 节点而不是集合本身(请参阅 https://developer.mozilla.org/en-US/docs/DOM/HTMLCollection)。此外,您不会尝试操作集合列表,因此,如果您确实使用数组将数据推送到,请确保它们是单独的数据而不是 HTMLCollection。
jquery中的解决方案:
$("input[name=trailer_use]").click(function(event){
if(event.target.id == "trailer_use_yes"){
$("#hidden_auto_questions").removeClass('hide_auto').addClass('show_auto');
$("input[name=trailer_insured]").removeAttr("disabled");
show_trailer_warning(null, "remove");
}else {
$("#hidden_auto_questions").removeClass('show_auto').addClass('hide_auto');
$("input[name=trailer_insured]").attr("disabled", "disabled");
if($("#hidden_include_auto").hasClass("show_auto")){
$("#hidden_include_auto").removeClass('show_auto').addClass('hide_auto');
$('input[name="custom_attributes[trailer_insurance_endorsement]"]').attr("disabled", "disabled");
}
show_trailer_warning(null, "disclaimer");
}
});
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 数组在递归方法中设置为null
- 打破承诺链的好方法是什么
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 使用“;这个“;JavaScript原型方法中的关键字
- 序列化数据属性中对象的最可靠方法
- 使用Objective-C的JavaScript注入方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 有没有一种方法可以防止img get请求使用css或js发生
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 在 JavaScript 中使用 setAttribute 方法
- 当有更好的方法时,使用 setAttribute 方法有什么意义
- 未捕获的类型错误:对象没有方法'setAttribute'
- javascript类型错误:无法调用方法'setAttribute'零
- 如何使用setAttribute方法
- onclick用于IE7的setAttribute解决方法
- IE9 错误:对象不支持属性或方法“setAttribute”
- 我应该如何正确使用setAttribute()方法
- 如何在javascript的setAttribute()方法中为函数添加参数