How do I prevent anchor tags for ajax requests from going to the top of the page?
This seems like a stupid question, but I can't figure it out. I have some anchor tags on my page that perform ajax functions only, and have no inherent use except for triggering javascript:
<a href="#" onclick="SomeFunction();">Blah</a>
How do I prevent clicking on this link from scrolling all the way to the 开发者_运维知识库top of the page. It is causing some annoying UI usability issues.
HTML
<a href="javascript:void(0);" id="theLink" onclick="SomeFunction();">Blah</a>
JS
//jQuery
$('#theLink').click(function(){
doSomething();
});
//Regular JS
document.getElementById("theLink").onclick = doSomething;
JSFiddle
http://jsfiddle.net/kSxrK/
Specify a non existing anchor such as <a href="#nogo" onclick="SomeFunction();">Blah</a>
Add return false
to your SomeFunction()
to the very end :)
var SomeFunction = function(){
//AJAX MAGIC
return false;
}
精彩评论