问题
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