开发者

How do I prevent anchor tags for ajax requests from going to the top of the page?

开发者 https://www.devze.com 2023-02-07 14:55 出处:网络
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:

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;
}
0

精彩评论

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