How does javascript-jquery disable the redirection of tag?

Source: Internet
Author: User
Under normal circumstances, I can add jacascript:; to the href of the a tag to prevent its click event. But this time I got a function and I cannot modify the href in it. Because I need the href address for ajax loading. How can I set this to enable this href to execute the onclick event without adding jacascript:; to the href of the a tag, I can prevent its click event.
But this time I got a function, and I cannot modify the href in it. Because I need the href address for ajax loading.
How can I configure this href to execute the onclick event without redirecting?

Reply content:

Under normal circumstances, I add jacascript:; to the href of a tag to prevent its click events.
But this time I got a function, and I cannot modify the href in it. Because I need the href address for ajax loading.
How can I configure this href to execute the onclick event without redirecting?

             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 the case. Do I have to write the address in href?
Pass function parameters

$ ("A"). on ("click", function (){

Return false; // What do you want?

})

Js has a function to prevent default behavior.
E. preventDefault ();
You can try it.

In fact, clicking the tag will jump, because the function triggered by clicking returns true by default, so when the onclick function returns false, it will not jump.

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

The execution function can only write return or not execute

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

In the click event

return false;

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