Will JQuery override existing inline event handlers?

跟風遠走 提交于 2020-01-25 04:21:05

问题


I have a site which is built out of many iframes. I am working on monitoring user activity - like when user clicks or keydown.

This is only to see if user is idle or not.

For this, I am drilling down to all iframes,its div tags and registering hover and click events. Meanwhile I dont want to override/break existing inline event handlers which are defined by the application.

Will jquery override exiting eventhandlers? If yes, how can check this to make sure I dont do this?

Here is my usage.

$(divElementObj).click( function() {
    alert("div click");
});

回答1:


No, jQuery works by using addEventListener/attachEvent. Inline and pre-existing handlers are not overwritten.

See jsFiddle example and the jQuery source to show how this is done.




回答2:


As long as you don't use something like event.stopPropagation(); you should be fine. event.stopPropagation(); could cause issues if your existing site is expecting events to bubble.




回答3:


There is a plugin for it, but you should only do this when you really need to.

jQuery Override Plugin

$(divElementObj).override('onclick', 'click', function(...));


来源:https://stackoverflow.com/questions/6050211/will-jquery-override-existing-inline-event-handlers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!