getAttribute cannot return class in IE7?

后端 未结 5 1657
暖寄归人
暖寄归人 2021-01-04 18:59

I need to find random nodes according to random attribute values. To do that I use getAtrribute on nodes from getElementsByTagName.

It seems like when I look for cl

5条回答
  •  一生所求
    2021-01-04 19:20

    Anyone know if getAtrribute doesn't work only on 'class' or other attributes as well?

    It fails for all attributes where the HTML attribute name differs from the DOM property name (className, htmlFor), plus you have to use the DOM-style capitalisation. It also returns the wrong datatype for attributes whose DOM properties aren't strings:

    disabled, readOnly, checked, selected, multiple,
    compact, declare, isMap, noHref, defer, noResize,
    size, cols, rows, width, height, hspace, vspace,
    maxLength, tabIndex, colSpan, rowSpan
    

    and possibly others I've missed!

    element.getAttribute(x)
    

    in IE is exactly the same as saying:

    element[x]
    

    So in general you should avoid using getAttribute, and use the simple DOM Level 1/2 HTML interfaces such as ‘element.className’ instead.

    This is finally fixed in IE8.

提交回复
热议问题