我有一个这样的按钮
<button data-cart-itemid="1be8718a-6993-4036-b7c6-8579e342675d" data-action="inc">
当我点击文档时,我需要检查它是否点击了那个按钮。我需要使用属性data-action="inc"
来检查它我试过这段代码,但它总是给我false
document.addEventListener('click', (e)=>{
console.log(e.target.closest('button').hasAttribute("[data-action='inc']"));
});
要扩展我的最后一个评论,您可以这样做
<body>
<button data-cart-itemid="1be8718a-6993-4036-b7c6-8579e342675d" data-action="inc">
<img src="https://www.webfx.com/wp-content/themes/fx/assets/img/footer/footer-roket.png">click it</span>
</button>
</body>
document.addEventListener('click', (e)=>{
console.log(e.target.activeElement)
});
https://jsbin.com/yifulebate/edit?html, js、控制台、输出