最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Cancel javascript onclick event in FF - Stack Overflow

programmeradmin3浏览0评论

I have linkbutton with onclick event. I want cancel onclick event in FF dinamically, sometimes user can click, sometime cant click. I want cancel the onclick event in JS function fired from other control(dropdown, textbox). So, I want to write function, when sometimes cancel this event and sometimes dont cancel this event from function fired from another element on the page.

Example:

function someFCE(linkButtonID)
{ 
  var linkButton = document.getElementById(linkButtonID);

  if (//firefox//)
 {
     if (//something//)
         //cancel event//
     }
 }

Where is //cancel event//, please help me write code to cancel this onclick event on FF.
And sorry my english.


If I understand right, I can cancel the event after click on the button. The problem is, the button has other JS validation and it is dangerous rewrite logic of this button.
If I add other click event with jquery which cancel this click event, this cancel event calls after this validation. So, is some possibility add cancel event which calls first before all validations or simply cancel onclick event? Only in FF Thanks.

I have linkbutton with onclick event. I want cancel onclick event in FF dinamically, sometimes user can click, sometime cant click. I want cancel the onclick event in JS function fired from other control(dropdown, textbox). So, I want to write function, when sometimes cancel this event and sometimes dont cancel this event from function fired from another element on the page.

Example:

function someFCE(linkButtonID)
{ 
  var linkButton = document.getElementById(linkButtonID);

  if (//firefox//)
 {
     if (//something//)
         //cancel event//
     }
 }

Where is //cancel event//, please help me write code to cancel this onclick event on FF.
And sorry my english.


If I understand right, I can cancel the event after click on the button. The problem is, the button has other JS validation and it is dangerous rewrite logic of this button.
If I add other click event with jquery which cancel this click event, this cancel event calls after this validation. So, is some possibility add cancel event which calls first before all validations or simply cancel onclick event? Only in FF Thanks.

Share Improve this question edited Sep 30, 2011 at 6:36 user50049 asked Sep 29, 2011 at 19:15 PimpyPimpy 511 gold badge1 silver badge10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You should add an event listener to the element:

linkButton.onclick = function(ev){
    ev.preventDefault();
    ev.stopPropagation();
}

The event object is passed as a first argument to an event listener. Methods of the event object can be invoked from within the event listener. preventDefault() prevents the default event from occuring. stopPropagation() cancels the bubble of the event, so that other event listeners don't receive the event any more.

If you want to stop the click event from firing, you can set the onclick event of the LinkButton to null:

linkButton.onclick = null;

This will remove the function that was previously there, so when the button is clicked, there is no event handler to be called.

发布评论

评论列表(0)

  1. 暂无评论