onmouseover:鼠标悬停在元素上的时候触发

onmouseout:鼠标移开的时候触发

eg:鼠标悬停时背景为红色,鼠标移开时背景为绿色

Video_2020-11-23_184616.wmv (67.21KB)

代码:

  1. <body>
  2. <p id="p"> hello world</p>
  3. <script>
  4. var p = document.getElementById("p");
  5. p.onmouseover =function(){
  6. this.style.background ="red";
  7. }
  8. p.onmouseout =function(){
  9. this.style.background ="green";
  10. }
  11. </script>
  12. </body>