给伪元素添加事件

方式一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>

 

上一篇:二、Nginx操作


下一篇:webpack中clean-webpack-plugin插件使用遇到的问题及解决方法