如何使用纯Javascript检查单击的元素是否具有后台属性?
我想检查我在页面上单击的元素是否具有后台属性,这是我尝试的代码如何使用纯Javascript检查单击的元素是否具有后台属性?,javascript,Javascript,我想检查我在页面上单击的元素是否具有后台属性,这是我尝试的代码 var img = document.querySelectorAll('*'); img.forEach(function(image){ image.addEventListener("click",function(e){ e.preventDefault(); if(this.style.background){ console.log("yes"); }
var img = document.querySelectorAll('*');
img.forEach(function(image){
image.addEventListener("click",function(e){
e.preventDefault();
if(this.style.background){
console.log("yes");
}
});
});
试试这个:
if(e.target.style.background){
console.log("yes");
}
看
我会成功的
顺便说一句,我还添加了一个e.stopPropagation();防止DOM中冒泡
window.getComputedStyle(this , null).getPropertyValue( "background-image" )