繁体   English   中英

在ul javascript中获取图片位置

[英]Getting an images position within a ul javascript

我试图例如用ul获取图像的位置:

function myFunction(){
  var img = document.getElementById("myID").querySelectorAll("img");
  var x = x;
  img[x].style.backgroundColor = "red";
}

当有人单击图像时,该功能将运行。

我想要一种方法来获取var x =单击的图像数。

我不知道jQuery,所以如果它全部在平面javascript中,我将不胜感激。

多数民众赞成在一个XY问题。 大概:

//wait for all clicks
window.addEventListener("click",function(event){
//check if it has a parent with id myID
  var e=event.target;
  while(e!==document.body && e.id!="myID"){
      e=e.parent;
  }
 if(e.id=="myID"){
   //set a backgroundcolor to the clicked element. May also check if Node is IMG
    event.target.style.backgroundColor="red";
}
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM