猿问

为什么这段代码是先执行alert("i'm "+this.className);求指导,谢谢

为什么这段代码是先执行alert("i'm "+this.className);再执行this.style.backgroundColor="yellow";我想要的效果是先背景改变,再跳出alert,点确认后又变回原来的背景,求指导,谢谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 <title>Document</title>
 <style>
  *{
   padding: 0;
   margin: 0;
  }
  .id1
  { 
   position: absolute;
   background-color: red;
   width: 200px;
   height: 200px;
   z-index: -1;   
  }
  .id2
  {
   position: absolute;
   background-color: blue;
   width: 150px;
   height: 150px;
   top: 25px;
   left: 25px;
  }
  .id3
  {
   position: absolute;
   background-color: green;
   width: 100px;
   height: 100px;
   top: 25px;
   left: 25px;
  }
 </style>
 <script>
  function lightHight() {
   this.style.backgroundColor="yellow";
   alert("i'm "+this.className);
   this.style.backgroundColor="";
   
   // body...
  }
  window.onload=function(){
   var divs=document.getElementsByTagName("div");
   divs[0].addEventListener("click",lightHight);
   divs[1].addEventListener("click",lightHight);
   divs[2].addEventListener("click",lightHight);
  } </script>
</head>
<body>
 <div>
  
  <div>
   
   <div>
    
   </div>
  </div>
 </div>
</body>
</html>

慕粉3834220
浏览 1798回答 2
2回答

lkepanda

我也是背景颜色出不来

guoyou

效果确实是先背景改变,再跳出alert,点确认后又变回原来的背景   并没有问题
随时随地看视频慕课网APP
我要回答