admin管理员组文章数量:1425938
I'm sorry if I worded the title wrong, I didn't know how to say it in short.
My problem is, I have this code:
document.addEventListener("click",changeDesc,true);
function changeDesc() {
if(event.target.className == "item")
{
var CurrentItem = document.getElementById(event.target.id);
var CurrentDescription = CurrentItem.getElementsByClassName("itemdescription")[0].textContent;
var ItemBox = document.getElementsByClassName("infobox")[0];
ItemBox.innerHTML = CurrentDescription;
}
}
that checks if the thing the user clicked on is a class called 'item', and if it is, it changes the text contents of a div.
The item is formatted like this:
<li class="item" id="iron_sword">
<img src="1.png" />
<p class="itemdescription">Description 1</p>
</li>
If I click the area that is covered by the 'li' element itself (stretches horizontally fully), it works perfectly. However, if I try to click the image INSIDE of the li (image 1.png), it doesn't do anything.
This confuses me because the image is inside of the li, but I think I get why it's happening.
But what I don't get is how I should fix this. Here's a jsfiddle so you can tinker with the code and if you don't understand, maybe you can see what I mean.
/ (Try to click on any area directly to the left or the right of the images, aka the full area of the li. You can see it does what I want it to do, but it doesn't work if I click the images themselves.
NOTE: I'm not using jquery. My javascript is external.
I'm sorry if I worded the title wrong, I didn't know how to say it in short.
My problem is, I have this code:
document.addEventListener("click",changeDesc,true);
function changeDesc() {
if(event.target.className == "item")
{
var CurrentItem = document.getElementById(event.target.id);
var CurrentDescription = CurrentItem.getElementsByClassName("itemdescription")[0].textContent;
var ItemBox = document.getElementsByClassName("infobox")[0];
ItemBox.innerHTML = CurrentDescription;
}
}
that checks if the thing the user clicked on is a class called 'item', and if it is, it changes the text contents of a div.
The item is formatted like this:
<li class="item" id="iron_sword">
<img src="1.png" />
<p class="itemdescription">Description 1</p>
</li>
If I click the area that is covered by the 'li' element itself (stretches horizontally fully), it works perfectly. However, if I try to click the image INSIDE of the li (image 1.png), it doesn't do anything.
This confuses me because the image is inside of the li, but I think I get why it's happening.
But what I don't get is how I should fix this. Here's a jsfiddle so you can tinker with the code and if you don't understand, maybe you can see what I mean.
https://jsfiddle/c36uwwmr/1/ (Try to click on any area directly to the left or the right of the images, aka the full area of the li. You can see it does what I want it to do, but it doesn't work if I click the images themselves.
NOTE: I'm not using jquery. My javascript is external.
Share Improve this question edited Mar 13, 2017 at 10:53 Nope 22.3k8 gold badges49 silver badges73 bronze badges asked Mar 13, 2017 at 10:52 SnakeBlockSnakeBlock 231 gold badge1 silver badge3 bronze badges 1-
document.getElementById(event.target.id);
is pretty pointless. You should just useevent.target
– Bergi Commented Mar 13, 2017 at 13:11
1 Answer
Reset to default 1if I try to click the image INSIDE of the li (image 1.png), it doesn't do anything.
Of course, not because then the event.target
is the image element not the list item with the class. If you want to do event delegation, you have to check all the ancestors of the event target. You'll want to do something like
function changeDesc() {
for (var currentItem = event.target
;currentItem != event.currentTarget && currentItem != null
;currentItem = currentItem.parentElement
) {
if (currentItem.className == "item") { // or currentItem.classList.contains("item")
// or currentItem.matches(".item")
…
break;
}
}
}
(see updated demo)
本文标签:
版权声明:本文标题:javascript - How would I make event.target.className check for an image inside of the div named className? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745456734a2659136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论