监听

2019-05-07  本文已影响0人  小福福_b2b1

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

例子:

<!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>

上一篇 下一篇

猜你喜欢

热点阅读