日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JS中获取元素的第二种方法

發布時間:2023/12/20 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JS中获取元素的第二种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.靜態方法

  var oUl = document.getElementById('');

2.動態方法

  document.getElementsByTagName('');

?

1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>無標題文檔</title> 6 <script> 7 /* 8 #list {} var oUl = document.getElementById('list'); 靜態方法 9 10 li {} document.getElementsByTagName('li'); 動態方法 11 #list li {} var aLi = oUl.getElementsByTagName('li'); 12 // aLi => [ li, li, li ] 元素的集合 13 aLi.length 3 14 aLi[0] 15 // 在用 TagNasme 的時候,必須要加上:[] 16 */ 17 window.onload = function (){ 18 // var oUl = document.getElementById('list'); 19 var oUl = document.getElementsByTagName('ul')[0]; 20 var aLi = oUl.getElementsByTagName('li'); 21 22 // document.getElementsByTagName('li'); 23 24 // alert( aLi.length ); 25 }; 26 </script> 27 </head> 28 29 <body> 30 31 <ul id="list"> 32 <li></li> 33 <li></li> 34 <li></li> 35 </ul> 36 37 <ol> 38 <li></li> 39 <li></li> 40 </ol> 41 42 </body> 43 </html> 示例代碼 1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>無標題文檔</title> 6 7 <script> 8 window.onload = function (){ 9 // document.title = 123; 10 // document.body.innerHTML = 'abc'; 11 var aBtn = document.getElementsByTagName('input'); 12 13 // alert(aBtn.length); 14 15 document.body.innerHTML = '<input type="button" value="按鈕" /><input type="button" value="按鈕" /><input type="button" value="按鈕" />'; 16 17 // alert(aBtn.length); 18 aBtn[0].onclick = function (){ alert(1); }; 19 aBtn[1].onclick = function (){ alert(1); }; 20 aBtn[2].onclick = function (){ alert(1); }; 21 22 // 重復執行某些代碼 23 // 每次執行的時候,有個數字在變化 24 }; 25 </script> 26 27 </head> 28 29 <body> 30 </body> 31 </html> 小練習題

?

轉載于:https://www.cnblogs.com/123wyy123wyy/p/6901547.html

總結

以上是生活随笔為你收集整理的JS中获取元素的第二种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。