监听

作者: 小福福_b2b1 | 来源:发表于2019-05-07 15:28 被阅读0次

用监听挂事件的好处:可以挂多个事件而不被替换

例子:

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8"/>

<title>监听器</title>

<script src="http://www.jq22.com/jquery/1.4.4/jquery.min.js"></script>

</head>

<style type="text/css">

  #areaDIV{

  height: 200px;

  width: 200px;

  background-color: black;

  }

  #showMSG{

  height: 200px;

  width: 200px;

  background-color: red;

  }

</style>

<body>

<div id="areaDIV">

<p></p>

</div>

<div id="showMSG">

</div>

<script type="text/javascript">

// 方法一 监听器

//var areaDIV=document.getElementById("areaDIV");

//var showMSG=document.getElementById("showMSG");

    //areaDIV.onclick=changecolor;

    var changecolor=function(){

    var showMSG=document.getElementById('showMSG');

    showMSG.style.backgroundColor="black";

    }

    //方法二 添加监听器 可以挂多个事件

    document.body.addEventListener('click',changecolor);

</script>

</body>

</html>

相关文章

网友评论

      本文标题:监听

      本文链接:https://www.haomeiwen.com/subject/nqaeoqtx.html