div阻止js鼠标点击事件穿透

1,877 阅读1分钟
阻止点击穿透,点击上层,为下层添加下面样式代码即可
pointer-events: none;

完整例子代码如下:

<!DOCTYPE html>
<html>
<head>
  <title>阻止点击穿透</title>
  <style>
    div{
      float: left;
    }
    .under{
      width: 300px;
      height: 150px;
      padding: 20px;
      margin: 20px;
      background: #5b7e91;
    }
    .up{
      width: 50px;
      height: 50px;
      line-height: 50px;
      border-radius: 10px;
      background: #ec6800;
      text-align: center;
      color: #fff;
    }
    .noclick{
      pointer-events: none;  /* 上层加上这句样式可以阻止点击穿透 */
    }
  </style>
</head>
<body>
  <div class="under" onclick="under()">
    <p>阻止点击穿透</p>
    <div class="up noclick" onclick="up()">点我</div>
  </div>
  <script>
    var under = function() {
      alert("点击上层")
    }
    var up = function() {
      alert("点击下层")
    }
  </script>
</body>
</html>

效果如下:

image.png