attr函数

attr 函数 就是给元素添加属性

属性有2中 

元素本身就有的                       如 name  id  =======

还有就是用户自定义属性       如 vp

 

然后ie下的属性设置 跟 其他的浏览器有些区别   简单记录一下

 

设置元素的style

ie6,7下面

元素不能直接设置style这属性

必须用    元素样式的 cssText  来设置值

一个例子

 <a href="#" id=a2">123123</a>

ie 6,下面  

a.style.setAttribute('cssText',"color:#f00")

非ie6,7 下面

a.setAttribute('style',"color:#f00")

 

修改input的type

ie 6,7,8下面(9没有测试)  已经添加到dom树上的input是不允许修改type的

非ie可以

 

一些元素的属性  如 href|src|style

在ie下面 写相对路径的时候取值  会出现绝对路径

有种用法

a.getAttribute('href',2)

第2个参数为0,1,2

参考这个文章

还有透明度 不一样

 

 

 

上面瞎篡改代码

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>attr</title> 
<link href="manage.css" mce_href="manage.css" rel="stylesheet" type="text/css" /> 
</head> 
 
<a id="a2">sssssss</a> 
<div id="a1"></div> 
<body> 
<script type="text/javascript"> 
(function(){     
    var div = document.createElement("div"); 
    div.innerHTML = '   <link/><table></table><a href="/a" mce_href="a" style="color:red;float:left;opacity:.5;" mce_style="color:red;float:left;opacity:.5;">a</a><select><option>text</option></select><object><param/></object>'; 
    var a = div.getElementsByTagName("a")[0]; 
    window.support = { 
        style          : /red/.test(a.getAttribute("style")), 
        opacity        : a.style.opacity === "0.5", 
        hrefNormalized : a.getAttribute("href") === "/a" 
    };   
})(); 
 
function attr(elem, name, value){ 
    if (!elem || elem.nodeType == 3 || elem.nodeType == 8) 
        return undefined; 
         
    var set = value !== undefined;   
     
    //如果elem是元素  
    if(elem.tagName){ 
        //这几个元素在ie下面要进行特殊的处理  
        var special = /href|src|style/.test(name);       
         
        //if (name == "selected" && elem.parentNode)  
            //elem.parentNode.selectedIndex;          
         
        //如果是元素原有的属性  
        if (name in elem && !special){ 
            //存在 value值的情况  
            if (set){ 
                //ie下面无法修已经存在的input 的 type  
                if ( name == "type" && elem.nodeName==="INPUT" && elem.parentNode ) 
                    throw "type property can't be changed"; 
                 
                elem[ name ] = value; 
            } 
             
            //如果不存在value  这个时候是获取这个值了     
            if( elem.nodeName==="FROM" && elem.getAttributeNode(name) ) 
                return elem.getAttributeNode(name ).nodeValue; 
             
            return elem[name];       
        } 
         
        //在ie6,7下面不能直接用attribute来操作样式 所以用cssText  
        if (!support.style &&  name == "style") 
            return arguments.callee( elem.style, "cssText", value );             
         
        //如果不是元素原有的属性 且有值    
        if(set) 
            elem.setAttribute(name, "" + value); 
 
        //href|src|style 设置属性得用elem.getAttribute( name,2)  
        var attr = !support.hrefNormalized && special 
                // Some attributes require a special call on IE  
            ? elem.getAttribute( name,2) 
            : elem.getAttribute( name ); 
         
        return attr === null ? undefined : attr;                         
    } 
     
    if (!support.opacity && name == "opacity" ) { 
        if (set) { 
            elem.zoom = 1;  
            elem.filter=(elem.filter || "").replace( /alpha\([^)]*\)/, "" )+  (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); 

        } 
 
        return elem.filter && elem.filter.indexOf("opacity=") >= 0 ? 
            (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '': 
            ""; 
    } 
     
    //这个后面的主要应用于样式 elem.style  
    name = name.replace(/-([a-z])/ig, function(all, letter){ 
        return letter.toUpperCase(); 
    }); 
 
    if (set) 
        elem[ name ] = value; 
 
    return elem[name];       

 
window.onload = function(){ 
    var div = document.getElementById('a1'), 
        a   = document.getElementById('a2'); 
    attr(div,'n',12312323); 
    attr(div,'style','height:200px;width200px'); 
    attr(div.style,'background-color','#ff0000'); 
    attr(div.style,'opacity',0.2); 
    attr(a,'href','http://www.baidu.com'); 

</script>  
</body> 
</html> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值