Javascript-jquery how to make a tag do not jump?

Source: Internet
Author: User
Under normal circumstances, I add jacascript to the href of a tag:; You can block its click events.
But this time I got a function and I can't modify the href inside. Because I need an href address to do Ajax loading.
How can I set this href to perform the onclick event without jumping?

Reply content:

Under normal circumstances, I add jacascript to the href of a tag:; You can block its click events.
But this time I got a function and I can't modify the href inside. Because I need an href address to do Ajax loading.
How can I set this href to perform the onclick event without jumping?

             var _this = $(this);             var subHref = _this.attr('href');             e.preventDefault();                        $.ajax({                 url: '1.html',                 data: formVal,                 type: post,                 dataType: 'json',                 success: function(res) {                     if (res.code == 200) {                         location.href = subHref;                     } else {                                 }                 }                 error: function() {                             }             });

href= "javascript:;" onclick= "MyFunction (" http://shabi.com ")"

This is not going to work, the address must be written in the href?
Do the function parameter passing.

$ ("a"). On ("click", Function () {

return false;//想干嘛,干嘛

})

JS has a function to block the default behavior
E.preventdefault ();
You can try it.

In fact, clicking on the a tag jumps, because clicking on the triggered function returns true by default, so when the return value of the OnClick function is modified to false, it will not jump.

var a=document.getElementById("a_id");a.onclick=function(e){//do something for myselfreturn false;}

The execution function writes only return and can not execute

var a=document.getElementById("a_id");a.onclick=function(e){return;}

In the Click event, directly

return false;

Can

  • 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.