JQuery cancels subsequent execution. jquery cancels subsequent execution.

Source: Internet
Author: User

JQuery cancels subsequent execution. jquery cancels subsequent execution.

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Script type = "text/javascript">
// Click the tag and the page is not redirected.
Window. onload = function (){
Var obj = document. getElementById ("myhref ");
Obj. onclick = function (event ){
// Cancel the default action
// Return false;
// Separate browsers
// Under IE
// 01. First capability Detection
Event = event | window. event;
If (event. preventDefault ){

// Non-IE
Event. preventDefault ();
} Else {
Event. returnValue = false;
}
};
}
</Script>
</Head>
<Body>
<A id = "myhref" href = "http://www.baidu.com"> go to Baidu </a>
</Body>
</Html>

Here, the click Effect of tag a is to jump to the Baidu page, but we can cancel the default behavior through parameters so that subsequent content of this click event will not be executed.

Sometimes there will be multiple events after clicking on the same tag. If you only want to execute the first event and discard the subsequent event, you can add a piece of code to prevent it:

<Span onclick = "alert ('will not be executed later! '); Event. stopPropagation (); "> click me! </Span>

 

How does jquery Block Post-bound events?

Your code has completed event binding during page loading. There is no way to block events that will be bound later. However, you can delete event binding for the current specified node. The method is as follows:
$ ("# Btn"). click (function (){
If ($ ("# tx"). val () = ""){
Alert ("e1 ");
} Else {
// Events bound after deletion...
$ ("# Btn"). unbind ('click ');
}
});

Note:
Unbind ([type], [data])
Bind () reverse operation to delete binding events from each matching element.
If no parameter exists, all bound events are deleted.

You can unbind the custom event you registered with bind.

If the event type is provided as a parameter, only binding events of this type are deleted.

If the handler passed during binding is used as the second parameter, only this specific event handler will be deleted.
 

Jquery: How can I block the second click event?

Jquery provides a method to trigger only one click.
Obj. one (function (){

});
Or you can use obj. unbind ("click") to cancel the click event.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.