简体   繁体   English

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");
                }
            }
        }
    }
}

Works perfectly fine in Chrome and Firefox, but in IE9 the console gives me an error on this line: 在Chrome和Firefox中可以正常工作,但是在IE9中,控制台在此行给我一个错误:

inputs_list[inputs][input].setAttribute("disabled", "disabled")

If I console.log(inputs_list[inputs][input]) I get this in Chrome: 如果我console.log(inputs_list[inputs][input])我在Chrome中得到这个:

(The input is being generated by RoR, which is why its name and id are so long) (输入是由RoR生成的,这就是其nameid如此长的原因)

<input class="radio_buttons optional" id="custom_attributes_trailer_insurance_endorsement_false" name="custom_attributes[trailer_insurance_endorsement]" type="radio" value="false" disabled="disabled"> 

Yay, that's what I wanted (and expected)... But in IE9's console I get this: 是的,这就是我想要的(和预期的)...但是在IE9的控制台中,我得到了:

[object HTMLCollection]

Which is completely useless.... 完全没用...

So question: Where am I going wrong in IE's exalted view? 那么问题来了:在IE的崇高观点中,我哪里出问题了? I know IE9 supports setAttribute , so I am assuming it has to do with my for...in loops. 我知道IE9支持setAttribute ,所以我假设它与我的for...in循环有关。

Edit: So due to comments, the problem could be with the object types ( HTMLCollection being stored in an Array ) . 编辑:因此,由于注释,问题可能出在对象类型上( HTMLCollection存储在Array )。 If that is indeed the case, how do I go about making everything compatible data types? 如果确实如此,我该如何使所有兼容的数据类型呢?

HTMLCollections are not really arrays, so you may be getting inconsistent results in treating them that way. HTMLCollections并不是真正的数组,因此以这种方式处理它们可能会得到不一致的结果。 While all browsers will return an HTMLCollection from getElementsByTagName, there may be crucial differences in how they allow you to treat them. 尽管所有浏览器都会从getElementsByTagName返回HTMLCollection,但是在允许您使用它们的方式上可能存在重大差异。 The best approach is to use the DOM methods to access things from the HTMLCollection - so you would use .item(0) to make sure you are referencing a dom node and not the collection itself (see https://developer.mozilla.org/en-US/docs/DOM/HTMLCollection ). 最好的方法是使用DOM方法访问HTMLCollection中的内容-因此,您将使用.item(0)来确保您引用的是dom节点,而不是集合本身(请参阅https://developer.mozilla.org / en-US / docs / DOM / HTMLCollection )。 Also, you wouldn't try to manipulate a collection list so if you do use arrays to push data onto, make sure they're separate data and not an HTMLCollection. 另外,您不会尝试操作集合列表,因此,如果您确实使用数组将数据推入到列表中,请确保它们是单独的数据而不是HTMLCollection。

Solution in jquery: 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");
        }
  });

暂无
暂无

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

相关问题 对象不支持此属性或方法 -- 在 IE9 中调用 Applet - Object doesn't support this property or method -- Calling an Applet in IE9 对象不支持IE9中的属性或方法'append' - Object doesn't support property or method 'append' in IE9 IE9:对象不支持属性“是” - IE9: Object doesn't support the property “is” Fineuploader IE9 CORS iframe错误“对象不支持属性或方法&#39;_parseJsonResponse&#39; - Fineuploader IE9 CORS iframe error "object doesn't support property or method '_parseJsonResponse' IE9中的新XMLHttpRequest导致JScript运行时错误:对象不支持此属性或方法 - new XMLHttpRequest in IE9 causes a JScript runtime error: Object doesn't support this property or method Microsoft JScript运行时错误:对象不支持IE9中的属性或方法&#39;getElementsByTagName&#39; - Microsoft JScript runtime error: Object doesn't support property or method 'getElementsByTagName' in IE9 TypeError:对象在IE8 / IE9中不支持此属性或方法 - TypeError: Object doesn't support this property or method in IE8 / IE9 对象不支持IE9和IE8中的属性或方法“切换” - Object doesn't support property or method 'Toggle' in IE9 and IE8 Angular 4 IE9文件上传Script438:对象不支持属性或方法“应用” - Angular 4 IE9 file upload Script438:Object doesn't support property or method 'apply' ExtJs4 + IE9 = Object不支持属性或方法&#39;createContextualFragment&#39; - ExtJs4 + IE9 = Object doesn't support property or method 'createContextualFragment'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM