On IE document.getElementsByName won't work

前端 未结 2 1800
孤独总比滥情好
孤独总比滥情好 2020-12-10 15:54

I use this code:

2条回答
  •  攒了一身酷
    2020-12-10 16:41

    Using getElementsByName to get a DOM Element where the name attribute is not part of the W3C spec (eg, in the question, name doesn't exist for DIV element), IE doesn't get those elements. FF does it.
    Just to clarify: expando attribute or better known as custom attribute is what I am talking about attributes that are not part of the W3C spec.

    Read: getElementsByName in IE7
    Read: http://msdn.microsoft.com/en-us/library/ms536438(VS.85).aspx

    So in conclusion:
    Use getElementsByName when trying to get "Form Controls" (input, select, textarea) because they have name as an attribute according to the spec.
    If the elements are not Form Controls, use getElementById instead.

提交回复
热议问题