* 标签为 'DOM' 的日志

IE与Firefox处理Attribute的几点差异

前端开发中,经常需要动态的添加、移除或者获取元素的Attribute。也就是说经常会用到setAttribute、removeAttribute和getAttribute。今天要讨论的是开发中遇到的几处IE与Firefox对Attribute操作的差异。

属性名大小写

在Firefox中,属性没有小写的概念,就算属性名全用大写,Firefox也会解析成小写,用Firebug看就能看到。所以下面的代码在Firefox与IE中运行结果会不一样。

<div altStr="sss"></div>
<script type="text/javascript">
 var div = document.getElementsByTagName("div")[0];
 div.removeAttribute("altstr");
 alert(div.getAttribute("altstr"));//IE中返回sss,FF中返回null
</script>

不过在IE中,removeAttribute有第二个参数,设置为true表示不忽略大小写,为false时忽略大小写,默认值是true;Firefox中因为解析时就不存在大写属性了,所以就没有第二个参数。也就是说IE中removeAttribute(”test”,false)等同于Firefox中的removeAttribute(”test”),IE中removeAttribute(”test”,true)在Firefox中无法实现。

Continue Reading »