jsDemo06.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .mydiv01{
  8. margin: 100px auto;
  9. width: 300px;
  10. height: 300px;
  11. background-color: red;
  12. }
  13. button{
  14. width: 70px;
  15. height: 50px;
  16. color: #fff;
  17. background-color: #ff9500;
  18. }
  19. .mybtn{
  20. width: 143px;
  21. }
  22. .show{
  23. width: 290px;
  24. height: 50px;
  25. background-color: #0f0f00;
  26. color: #fff;
  27. font-size: 30px;
  28. text-align: right;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="show">0</div>
  34. <div>
  35. <button>7</button>
  36. <button>8</button>
  37. <button>9</button>
  38. <button>*</button>
  39. </div>
  40. <div>
  41. <button>4</button>
  42. <button>5</button>
  43. <button>6</button>
  44. <button>-</button>
  45. </div>
  46. <div>
  47. <button>1</button>
  48. <button>2</button>
  49. <button>3</button>
  50. <button>+</button>
  51. </div>
  52. <div>
  53. <button class="mybtn">0</button>
  54. <button>.</button>
  55. <button>3</button>
  56. </div>
  57. <div>
  58. 我的输入框: <input type="text" name="myText" class="myText" id="myText">
  59. </div>
  60. <div class="mydiv01" id="mydiv01"></div>
  61. <div class="mydiv01" id="mydiv02"></div>
  62. <script>
  63. //var 修饰符,是用来修饰变量的,var修饰的变量,这个变量就是全局变量。所有地方都可以访问它。
  64. //根据id获取DOM元素
  65. var myText = document.getElementById("myText");
  66. //获取焦点事件
  67. myText.onfocus=function (){
  68. console.log("我获得焦点了")
  69. }
  70. //失去焦点事件
  71. myText.onblur=function (){
  72. console.log("我失去焦点了")
  73. }
  74. //按下键盘后抬起事件
  75. myText.onkeyup=function (event){
  76. console.log(event);
  77. console.log(event.key); //获取按键的字符
  78. }
  79. //根据class获取DOM元素的集合
  80. //var mydivs=document.getElementsByClassName("mydiv01");
  81. //var mydiv=mydivs[0];
  82. //let let修饰的变量就是局部变量,只有函数内部才能访问。
  83. let mydiv=document.getElementById("mydiv01");
  84. mydiv.onclick=function (){
  85. console.log("鼠标点击了这个div")
  86. }
  87. mydiv.ondblclick=function (){
  88. console.log("鼠标 双 击了这个div")
  89. }
  90. mydiv.onmouseover=function (){
  91. console.log("鼠标移入这个div")
  92. }
  93. mydiv.onmouseout=function (){
  94. console.log("鼠标从这个div移出了")
  95. }
  96. </script>
  97. </body>
  98. </html>