2_练习.html 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #div1 {
  10. width: 200px;
  11. height: 200px;
  12. background: red;
  13. top: 200px;
  14. left: 100px;
  15. position: absolute
  16. }
  17. </style>
  18. <script>
  19. //等页面所有的东西全部加载完毕,执行里面的内容
  20. // window.onload = function () {
  21. // var btn = document.getElementById('btn')
  22. // console.log(btn)
  23. // }
  24. </script>
  25. </head>
  26. <body>
  27. <button id="btn">点击</button>
  28. <div id="div1">
  29. <ul>
  30. <li>1</li>
  31. <li>2</li>
  32. <li>3</li>
  33. </ul>
  34. </div>
  35. <script>
  36. var btn = document.getElementById('btn')
  37. console.log(btn)
  38. // var div1 = document.getElementById('div1')
  39. // btn.onclick = function(){
  40. // div1.style.background = 'blue'
  41. // div1.style.top = '400px'
  42. // div1.style.left = '250px'
  43. // }
  44. var uli = document.getElementsByTagName('li')
  45. for (var i = 0; i < uli.length; i++) {
  46. uli[i].index = i
  47. console.log(uli[2].index)
  48. }
  49. </script>
  50. </body>
  51. </html>