IE6和IE7是否支持setAttribute()函数

| 1036次阅读| 作者:js,javascript
摘要:IE6和IE7是否支持setAttribute()函数:setAttribute()函数可以设置对象的属性,如果不存在此属性,则会创建此属性。语法结构:el.setAttribute(name,value)参数列表:参数描述name必需。规定要设置的属性名。value必需。规定要设置的属性值。代码实...

IE6和IE7是否支持setAttribute()函数:
setAttribute()函数可以设置对象的属性,如果不存在此属性,则会创建此属性。
语法结构:

1el.setAttribute(name,value)

参数列表:

参数 描述
name 必需。规定要设置的属性名。
value 必需。规定要设置的属性值。

代码实例:

01<!DOCTYPE html>
02<html>
03<head>
04<meta charset=" utf-8">
05<meta name="author" content="http://www.softwhy.com/" />
06<title>setAttribute()函数-蚂蚁部落</title>
07<script type="text/javascript">
08window.onload=function(){
09  var mydiv=document.getElementById("mydiv");
10  mydiv.setAttribute("id","newid");
11  alert(mydiv.getAttribute("id"));
12}
13</script>
14</head>
15<body>
16<div id="mydiv"></div>
17</body>
18</html>

以上代码可以重新设置div的id属性值,并且弹出新设置的id属性值。
实例二:

01<!DOCTYPE html>
02<html>
03<head>
04<meta charset=" utf-8">
05<meta name="author" content="http://www.softwhy.com/" />
06<title>setAttribute()函数-蚂蚁部落</title>
07<script type="text/javascript">
08window.onload=function(){
09  var mydiv=document.getElementById("mydiv");
10  mydiv.setAttribute("newAttr","attrValue");
11  alert(mydiv.getAttribute("newAttr"));
12}
13</script>
14</head>
15<body>
16  <div id="mydiv"></div>
17</body>
18</html>

以上代码可以设置div的newAttr属性值,并且弹出此属性值。这里需要特别注意的是,因为div默认并不具有newAttr属性,这个时候setAttribute()函数会首先创建此属性,然后再给它赋值。

以上两个代码实例在各主流浏览器中都能够成功的执行,但这并不说明setAttribute()函数能够兼容各个浏览器。

再看一段代码实例:

01<!DOCTYPE html>
02<html>
03<head>
04<meta charset=" utf-8">
05<meta name="author" content="http://www.softwhy.com/" />
06<title>setAttribute()函数-蚂蚁部落</title>
07<style type="text/css">
08.textcolor{
09  font-size:18px;
10  color:red;
11}
12</style>
13<script type="text/javascript">
14window.onload=function(){
15  var mydiv=document.getElementById("mydiv");
16  mydiv.setAttribute("class","textcolor");
17}
18</script>
19</head>
20<body>
21  <div id="mydiv">蚂蚁部落</div>
22</body>
23</html>

以上代码,在标准浏览器中能够将字体大小设置为18px,字体颜色设置为红色,但是在IE6和IE7浏览器中却不能够生效。

不过依然可以使用mydiv.getAttribute("class")获取属性值"textcolor"。

也就是说在IE6或者IE7浏览器中,setAttribute()函数可以使用,但是并不是对所有的属性都有效。

下面就列举一下存在上述问题的属性:

1.class

2.for

3.cellspacing

4.cellpadding

5.tabindex

6.readonly

7.maxlength

8.rowspan

9.colspan

10.usemap

11.frameborder

12.contenteditable

13.style

为了解决上述问题就要写一个通用的跨浏览器的设置元素属性的接口方法:

01dom=(function(){
02var fixAttr={
03  tabindex:'tabIndex',
04  readonly:'readOnly',
05  'for':'htmlFor',
06  'class':'className',
07   maxlength:'maxLength',
08   cellspacing:'cellSpacing',
09   cellpadding:'cellPadding',
10   rowspan:'rowSpan',
11   colspan:'colSpan',
12   usemap:'useMap',
13   frameborder:'frameBorder',
14   contenteditable:'contentEditable'
15  },
16        
17  div=document.createElement('div');
18  div.setAttribute('class','t');
19        
20  var supportSetAttr = div.className === 't';
21        
22  return {
23   setAttr:function(el, name, val){
24    el.setAttribute(supportSetAttr ? name : (fixAttr[name] || name), val);
25   },
26   getAttr:function(el, name){
27    return el.getAttribute(supportSetAttr ? name : (fixAttr[name] || name));
28  }
29}
30})();

首先,标准浏览器直接使用原始属性名;其次,IE6/7非以上列举的属性仍然用原始属性名;最后这些特殊属性使用fixAttr,例如class。

那么上面的代码实例修改为以下形式即可:

01<!DOCTYPE html>
02<html>
03<head>
04<meta charset=" utf-8">
05<meta name="author" content="http://www.softwhy.com/" />
06<title>setAttribute()函数-蚂蚁部落</title>
07<style type="text/css">
08.textcolor{
09  font-size:18px;
10  color:red;
11}
12</style>
13<script type="text/javascript">
14dom=(function(){
15var fixAttr={
16  tabindex:'tabIndex',
17  readonly:'readOnly',
18  'for':'htmlFor',
19  'class':'className',
20   maxlength:'maxLength',
21   cellspacing:'cellSpacing',
22   cellpadding:'cellPadding',
23   rowspan:'rowSpan',
24   colspan:'colSpan',
25   usemap:'useMap',
26   frameborder:'frameBorder',
27   contenteditable:'contentEditable'
28  },
29          
30  div=document.createElement('div');
31  div.setAttribute('class','t');
32          
33  var supportSetAttr = div.className === 't';
34          
35  return {
36   setAttr:function(el, name, val){
37    el.setAttribute(supportSetAttr ? name : (fixAttr[name] || name), val);
38   },
39   getAttr:function(el, name){
40    return el.getAttribute(supportSetAttr ? name : (fixAttr[name] || name));
41  }
42}
43})();
44window.onload=function(){
45  var mydiv=document.getElementById("mydiv");
46  dom.setAttr(mydiv, 'class', 'textcolor');
47}
48</script>
49</head>
50<body>
51<div id="mydiv">蚂蚁部落</div>
52</body>
53</html>

以上代码可以在各主流浏览器中都有效,都可以将字体大小设置为18px,颜色设置为红色。
至于style属性可以使用el.style.color="xxx"这种形式进行兼容。

最原始地址是:http://www.softwhy.com/

返回顶部
学到老代码浏览 关闭浏览