其他分享
首页 > 其他分享> > 给伪元素添加事件

给伪元素添加事件

作者:互联网

方式一C3属性:给元素设置pointer-events: none ,给伪元素设置pointer-events: auto;
方式二:通过事件冒泡;
<buttom id="hhh" class="li">
    <span id="cont">哈哈哈</span>
  </buttom>
<style>
.li {
      height: 20px;
      background-color: #ccc;
      color: #000;
      /* pointer-events: none */
    }
    .li::after {
      content: 'X';
      color: red;
      /* pointer-events: auto */
    }
</style>

<script>
let dom = document.getElementById('hhh')
    dom.addEventListener('click', function (e) {
      e.stopPropagation()
      if (e.target.id == 'cont') {
        alert(1)
      }
      if (e.target.id == 'hhh') {
        alert(2)
      }
    })
</script>

 

标签:color,元素,id,添加,事件,li,hhh,pointer,events
来源: https://www.cnblogs.com/seize-today/p/15188488.html