开发者

Cancel javascript onclick event in FF

开发者 https://www.devze.com 2023-04-10 02:16 出处:网络
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 cont

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 even开发者_如何学Pythont? Only in FF Thanks.


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

精彩评论

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

关注公众号