getAttribute cannot return class in IE7?

后端 未结 5 1661
暖寄归人
暖寄归人 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:44

    You can grab a list of all attributes from your elements and test their value that way. This snippet handles both IE and WebKit browsers, and will return the string value of the CSS class:

    var value = "";
    var elements = document.getElementsByTagName("div");
    
    for(var i = 0; i < elements.length; i++){
        if(typeof elements[i].attributes['class'] == "undefined"){
            value = elements[i].getAttribute("class"); 
        } else {
          value = elements[i].attributes['class'].nodeValue;
        }
    
        alert(value); // careful, this will be a lot of alerts
    }
    

提交回复
热议问题