如何停止刷新页面的链接按钮 [英] How to stop link button refreshing the page

查看:58
本文介绍了如何停止刷新页面的链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我使用名为 updateLogButton 的链接按钮来显示/隐藏div。因为每次点击焦点移动到页面的开头时我都会使用链接按钮。如何停止此默认行为?

In my code I'm using a link button called updateLogButton which shows/hides a div. Because I use a link button everytime its clicked focus is moved to the beginning of the page. How can I stop this default behaviour?

Jquery片段:

$('#updateLogText').hide();

$('#updateLogButton').click(function() {

    if ($('#updateLogText').is(':visible')){

        //hide div if content is visible
        $('#updateLogText').fadeOut();

    }else{

        $('#updateLogText').fadeIn();       
    }

});

HTML code:

HTML code:

<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是传递给click处理程序的事件对象。

To prevent the default action when the link is clicked you can return false from the click handler or call event.preventDefault where event is the event object passed to the click handler.

$('#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;
});

这篇关于如何停止刷新页面的链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆