JavaScript 更改属性的指定值 ----setAttribute()方法

来源:互联网 发布:同步教育软件 编辑:程序博客网 时间:2024/06/07 15:21

setAttribute()方法把指定属性设置或更改为指定值。它同样有两个值,setAttribute(name,value),第一个是属性名字,第二个是设置或更改的指定值。

 <script>

function change(){
var inp=document.getElementById("btn2"); 
inp.setAttribute("type","text");

}
    </script>
 
  <input type="button" id="btn2" value="插入节点" onclick="change()">   
  

在上述例子中,将type为button(按钮)的input元素改为type为文本框(text),通过getElementById获取input对象,然后操作input对象的setAttribute()方法


当然,我们也可以直接更改input的type值。

 function change(){
var inp=document.getElementById("btn2"); 
inp.type="text";

}

获取input对象后,直接将input对象的type值改为text。这种方法也是可行的。

阅读全文
0 0
原创粉丝点击