0

在我的客户页面上,我想在每次用户点击某些东西时执行我的功能。函数将此元素标签名称发送到数据库。为了避免覆盖我使用的客户端页面点击事件处理程序mousedown。我的想法:

jQuery('body').on('mousedown', function(event){
    var that = event.target,
        thatTag = event.target.nodeName;

    jQuery.get( 
        "http://example.com",
        { parameter: thatTag },
        function() {
            //It hasn't time to execute, because mouseup is auto executed and click action change location (if I click anchor)
        }
    );
});

那么,是否有任何选项可以暂停/阻止mouseup/click直到我的 GET 操作完成?

4

1 回答 1

0

您需要在首次执行时使用.off取消绑定处理程序,然后在函数完成mousedown时再次重新绑定:.get

$(function() {
    // On page load, bind the first instance of the event
    $("body").on("mousedown", handleMouseDown);
});

function handleMouseDown(event) {
    // Detach the event so that is isn't handled again until the .get finishes
    $("body").off("mousedown", handleMouseDown);
    var that = event.target,
        thatTag = event.target.nodeName;

    jQuery.get( 
        "http://example.com",
        { parameter: thatTag },
        function() {
            // Re-attach the event now that the .get has finished
            $("body").on("mousedown", handleMouseDown);
        }
    );
}
于 2013-10-01T08:47:31.820 回答