开发者

Onclick event override with firebug

I have a开发者_JAVA百科dded an onclick event, which opens a new tab/page, to a button which has an action already (I don't know which action cause it isn't displayed in source, or maybe it's a form submit action).

Button originally also opens a page in new tab, what I want to do is fire up some function after my onclick attribute executes which will stop execution and that default page opening will not happen, only my page will load.

Is there something that can be done?


It sounds like the event is bubbling up after you have handled it. To stop this happening, add event.cancelBubble = true at the end of your handler.


Using jQuery, you can do this:

$('button').click(function() {

  //do something

  return false; // stop the event from propagating
});


Try getting your function that your call on the onclick event to return false. This will cause any existing action by the button to be overridden


If the handler was set using 'onclick=' you can simply rewrite the onclick property.

If a handler was added to an element with addEventHandler or attachEvent, you can remove it with removeEventHandler or detachEvent, using the same parameters that were used to set it.

If you don't know the setter, or if it used an anonymous function, you can't remove it.

You could replace the element with a duplicate that has no events and set your own on the new element.

(cloneNode is not supposed to clone events, but it sometimes does in some browsers.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜