在我的代码中,我使用一个名为 updateLogButton
的链接按钮,它显示/隐藏一个 div。因为每次将单击的焦点移至页面开头时,我都会使用链接按钮。我怎样才能停止这种默认行为?
Jquery 片段:
$('#updateLogText').hide();
$('#updateLogButton').click(function() {
if ($('#updateLogText').is(':visible')){
//hide div if content is visible
$('#updateLogText').fadeOut();
}else{
$('#updateLogText').fadeIn();
}
});
HTML 代码:
<tr>
<td><a href="#" id="updateLogButton">Update Log</a></td>
</tr>
<tr>
<td colspan="3" >
<div id="updateLogText" style="width:100%;">
<?php echo $data['updates']; ?>
</div>
</td>
</tr>
编辑: 我的意思的例子:http://jsfiddle.net/cF4Bb/7/
最佳答案
要阻止单击链接时的默认操作,您可以从单击处理程序返回 false 或调用 event.preventDefault
,其中 event 是传递给单击处理程序的事件对象。
$('#updateLogText').hide();
$('#updateLogButton').click(function(event) {
if ($('#updateLogText').is(':visible')){
//hide div if content is visible
$('#updateLogText').fadeOut();
}else{
$('#updateLogText').fadeIn();
}
event.preventDefault();
//or return false;
});
关于javascript - 如何停止链接按钮刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12795566/