Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用纯Javascript检查单击的元素是否具有后台属性?_Javascript - Fatal编程技术网

如何使用纯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" )