JS——DOM操作(操纵元素属性【getAttribute、setAttribute...】)


常见元素操作:

(1):oDiv.style.display='block';

(2):oDiv.style['display']='block';

(3):DOM方式  【看下面列举↓↓↓↓】

 

DOM方式操作元素属性

1》获取:getAttribute(' 名称 ')

2》设置:setAttribute(' 名称 ',' 值 ')

3》删除:removeAttribute(' 名称 ')

 

详细看如下代码:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 7     <title>Document</title>     
 8     <script>
 9         window.onload=function(){
10             var oTxt=document.getElementById('txt');
11 
12             oTxt.setAttribute('value','444');   //设置文本框的值为‘444’
13             
14             alert(oTxt.getAttribute('id'));     //获取文本框的id值  即:txt
15 
16             oTxt.removeAttribute('id');         //删除文本框的id值
17         }
18     </script>
19 </head>
20 <body>
21     <input type="text" id="txt">
22 </html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM