最近更新
阅读排行
关注本站

JS中的setAttribute方法

阅读:6984 次   编辑日期:2015-06-04

目录:

概述:

最近得有一个月没更新了吧,主要是因为工作太忙,基本没有休息,每天到家也23点以后了,在加上前一段时间去欧洲旅游了,也得有半个月。晒张玩去的图片吧~
五渔村
我们经常要用JS给Element添加属性,一般都用setAttribute方法,今天我们就来聊聊setAttribute。

setAttribute设置属性

   elementNode.setAttribute(name,value)//name属性名,value属性值
   var input = document.createElement("input");
    input.setAttribute("type", "text");
    input.setAttribute("name", "q");
    input.setAttribute("class",bordercss);

setAttribute设置事件

   var bar = document.getElementById("uw3c");
    bar.setAttribute("onclick", "javascript:alert('uw3c!');");
用setAttribute设置click属性很好理解,但是这会有兼容性问题,IE中不支持setAttribute设置事件属性,所以为了兼容,只能通过如下方法:
   document.getElementById("uw3c").onclick= function () { alert("uw3c!"); }
将本篇文章分享到:
top