控制页面在确认框的情况下移动到顶部

control page to move to top in case of confirm box

本文关键字:情况下 移动 顶部 确认 控制      更新时间:2023-09-26

以下是我的代码片段:

$(document).ready(function()
{
    $('table#example td a.delete').click(function()
    {
        if (confirm("Are you sure you want to delete this row?"))
        {
            alert("You Press OK");
        }
    });
}); 

我的网格视图位于页面底部。我按"确定"或"取消"按钮,页面移动到顶部。

我想保持不变的立场。如何控制这一点。

它实际上与confirm没有任何关系; 这是您单击链接的事实(我猜该链接中有href=""href="#"(。浏览器正在跟踪链接,这是链接click事件的默认操作。

您需要阻止默认操作,您可以通过从函数返回 false 或接受 click 函数的 event 参数并调用 event.preventDefault() 来实现。

返回false(既能阻止默认操作,又能阻止点击冒泡到祖先元素(:

$(document).ready(function()
{
    $('table#example td a.delete').click(function()
    {
        if (confirm("Are you sure you want to delete this row?"))
        {
            alert("You Press OK");
        }
        return false;
    });
});

使用 preventDefault(它只会阻止默认值,并且不会停止冒泡;祖先元素也会看到点击(:

$(document).ready(function()
{
    // Note argument -----------------------------v
    $('table#example td a.delete').click(function(event)
    {
        event.preventDefault();
        if (confirm("Are you sure you want to delete this row?"))
        {
            alert("You Press OK");
        }
    });
});