利用HtmlElement可以访问document中的元素,但是HtmlElement 仅公开所有元素共有的那些属性,如id/name/tagname等,对于未公开的属性,如href/alt等,则需要通过getAttribute()方法来获取。如要获取相册中的url,可以用下面的方法:
element.getAttribute(“href”) //该方法假定element已经对应<a href="space-album.html"></a>这个元素
但是对于<a href="space-3373845.html" class="loginName">忆龙2009</a>的元素,用element.getAttribute(“class”)却不能获取到class的值。
问题原因是不同浏览器所支持的属性不同,对于IE,要获取class属性,可用classname属性来替代,element.getAttribute(“classname”)
要解决不同浏览器对此的兼容性问题,可用以下类似的代码:
if (element.getAttribute(”className”) == “class的名字” ||element.getAttribute(”class”) == “class的名字”)
还有其他一些属性会有类似的兼容性问题。