I am trying to imitate jQuery.on("click", ".target", handler)
by utilizing addEventListener()
method. My research on Stackoverflow end up with this: Native addEventListener with selector like .on() in jQuery.
So, before some one mark my question as DUPLICATE please read carefully my requirements and my demo.
Here is my Plunker demo https://plnkr.co/edit/so8Sur?p=preview ( I am running chrome when testing it ). Following is the explanation for it:
- At purple box which is "jQuery way" is running as my expectation which should be show Message popup no matter i am clicking on
.target
box or it children which is.target-child
. - But for green box which is my custom event binding, with custom function:
customOn(selector, type, filter, handler)
, it will only show popup when click on.target
box only, but it will not show popup when i click on.target-child
.
So, my question is, I wonder what kind of magic used by jQuery to make event to be triggered when click on the .target-child
on my demo, while the actual selector filter is .target
. Will jQuery traverse up on DOM trees to check any selector match every time it needs to handle delegated event? If yes, i feel it will take enough computation resource ( maybe RAM ), is that right? I hope there will be much efficient way rather than doing traverse up like that.
Any answer and comment will be highly appreciated. Thanks in advance.