[英]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.