简体   繁体   English

点击事件绑定的怪异行为; 事件未触发

[英]Weird behaviour with on click event binding; event not firing

So I came across this weird issue and I don't know if I'm blatantly missing something. 所以我遇到了这个奇怪的问题,我不知道我是否公然缺少了一些东西。 Visit this page (or any medium article). 访问页面(或任何中型文章)。 Open console and inject the following JS code. 打开控制台并注入以下JS代码。

for (const elem of document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image')) {
    elem.addEventListener('click', function () { 
        console.log(event.target);
    });
} 

Now click on the big images, expected behaviour should be (please correct me cause I seem to be wrong) that the target element is printed when you click on it the first time and also the second time. 现在单击大图,预期的行为应该是(请纠正我,因为我似乎错了)在第一次和第二次单击时都打印了目标元素。 But as it turns out when you click on the image (zoomed) the second time (to zoom out) it doesn't print the target in the console. 但是事实证明,当您第二次单击(缩放)图像(缩放)时,它不会在控制台中打印目标。

I thought that there might be some overlay element and hence I bind the event on body to capture all of the events using the following injected JS. 我以为可能会有覆盖元素,因此我将事件绑定到body以使用以下注入的JS捕获所有事件。

document.body.addEventListener('click', function () {
    console.log(event.target);
}, true);

But even with that I only get one console print of the target. 但是即使那样,我也只能获得目标的一个控制台打印。

One theory for delegation using body not working might be following - The newly created element would not be in the body in its time of creation, it will be moved to its place in the DOM tree later on. 可能有一种关于使用不起作用的body进行委派的理论,可能是-新创建的元素在创建时就不会出现在body中,稍后将移至其在DOM树中的位置。 And hence delegation is not able to find it when did via body but able to capture it via document . 因此,委派无法通过body找到它,而是可以通过document捕获它。

After a bit more exploring and injecting the following JS (taken from here and I know break point can be added, I did do that earlier but to no end so resorted to this.) 经过更多的探索并注入了以下JS(从这里获取 ,我知道可以添加断点,我确实早做了,但一直没有结束,只能诉诸于此。)

var observer = new MutationObserver(function (mutationsList, observer) {
  for (var mutation of mutationsList) {
    if (mutation.type == 'childList') {
      console.log('A child node has been added or removed.');
    }
    else if (mutation.type == 'attributes') {
      console.log('The ' + mutation.attributeName + ' attribute was modified.');
    }
  }
});
observer.observe(document, {
  attributes: true,
  childList: true,
  subtree: true
});

I don't see any element being added to the DOM on click (it is being added on load) so that theory might not be correct. 我看不到单击时将任何元素添加到DOM(它是在加载时添加的),因此该理论可能是不正确的。 So I guess now the real question is why Event Capturing through document is able to find the click event where else not from body . 所以我想现在的真正问题是,为什么通过document事件捕获能够在其他地方而不是从body找到单击事件。 I don't think delegation works on initial DOM structure since it would break the purpose. 我不认为委派可用于最初的DOM结构,因为这样做会破坏目的。

Also if it is a duplicate please let me know, since I don't really know what to exactly search for. 另外,如果重复,请告诉我,因为我真的不知道要精确搜索什么。

probably because there is something in front of the zoomed image that intercepts the click event in capture mode and stops propagation. 可能是因为在缩放图像的前面有一些东西在捕获模式下拦截了click事件并停止了传播。

I've got success with this 我已经成功了

document.addEventListener("click", function(e){ console.log(e.target); }, true);

The image (you are trying to target) is dynamically made. 图像(您尝试定位)是动态制作的。 After you already clicked the image once you should be able to target it. 单击图像后,应该可以将其定位。

document.querySelectorAll('.progressiveMedia-image.js-progressiveMedia-image')

This queries the DOM for all elements that have both the class progressiveMedia-image and js-progressiveMedia-image . 这将在DOM中查询同时具有progressiveMedia-imagejs-progressiveMedia-image类的所有元素。 You iterate over the result and bind an event listener to the click event of each element. 您遍历结果并将事件侦听器绑定到每个元素的click事件。

When you do click on one of the images, the JavaScript that is already running in the page creates new elements and displays them. 当您单击其中一张图像时,页面中已在运行的JavaScript会创建新元素并显示它们。 These new elements might have the same classes, but did not exist originally when you searched the DOM. 这些新元素可能具有相同的类,但是在搜索DOM时最初并不存在。 As such, they do not have their click event bound. 因此,它们没有绑定click事件。

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

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