Return false within anchor tag gives error
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>
- 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.
- 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.
精彩评论