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