使用html:
<div id="dynamic-choice">
<a href="http://www.google.com/" class="ajaxLink">click me</a>
<a href="a.nother/page/toload.html" class="ajaxLink">click me 2</a>
</div>
<div id="dynamic-content">
<p>this will be replaced</p>
</div>和javascript
<script src="js/jquery-1.8.2.js"></script>
<script>
$(document).ready(function(){
$('a.ajaxLink').click(function(){
var url = $(this).attr('href');
$('#dynamic-content').empty().load(url);
});
return false;
});
</script>然而,锚点只是表现为正常的链接:/
发布于 2012-10-30 03:35:21
$('a.ajaxLink').click(function(e){
e.preventDefault();
var url = $(this).attr('href');
$('#dynamic-content').empty().load(url);
});是代码的工作版本。
你必须传递事件e才能使用preventDefault方法,以防止锚点行为正常!
Documentation:
event.preventDefault()
https://stackoverflow.com/questions/13128304
复制相似问题