开发者

Return false within anchor tag gives error

开发者 https://www.devze.com 2023-02-05 06:33 出处:网络
I have the following statement <a href=\"javascript: return false;\" id=\"addNew1\">ADD NEW ROW</a>

I have the following statement

<a href="javascript: return false;" id="addNew1">ADD NEW ROW</a>

If I click on this I get the following error in my browser.

return not in function

I use the 开发者_开发问答following JQuery function

$('#addNew,#addNew1').click(function(){

Do I need to include this in the function, the reason I do not add 'n # sign in is because the the page jumps to the top of the page.


<a href="#" onclick="return false;" id="addNew1">ADD NEW ROW</a>


  1. The href attribute should point to a real URL that will serve the same purpose as the JavaScript if the JavaScript should fail for any reason. Build on things that work.
  2. The click handler should return false so the link isn't followed if the JavaScript doesn't fail (at which point the value of href doesn't matter).


This works:

<a href="#" id="addNew1">ADD NEW ROW</a>

$('#addNew, #addNew1').click(function() {
    // do stuff
    return false;
}

Another option would be this:

$('#addNew, #addNew1').click(function(e) {
    // do stuff
    e.preventDefault();
}

Usually, if you want to prevent the default action, which in this case is the activation of the anchor, you do it inside the click handler.

0

精彩评论

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