【问题标题】:Why function not called on first click? [jQuery]为什么第一次点击时不调用函数? [jQuery]
【发布时间】:2015-12-02 00:03:23
【问题描述】:

为什么在第一次单击元素时不调用 toggleSidebar() 函数,仅在第二次等时调用?

    $( ".image .image-inner .zoomer-holder" ).click(function() {

        var isDragging = false;

        $(".image .image-inner .zoomer-holder")
        .mousedown(function() {
            isDragging = false;
        })
        .mousemove(function(e) {
            isDragging = true;
        })
        .mouseup(function(e) {


            var wasDragging = isDragging;
            isDragging = false;

            if (!wasDragging) {
                console.log("TAP");

                // **** FUNCTION **** //
                toggleSidebar();    

                $(".image .image-inner .zoomer-holder").unbind("mouseup");  

            } else {
                console.log("DRAG");
                $(".image .image-inner .zoomer-holder").unbind("mouseup");  
                $(".image .image-inner .zoomer-holder").unbind("mousedown");
                $(".image .image-inner .zoomer-holder").unbind("mousemove");                            
            }
        });
    });

【问题讨论】:

    标签: jquery click


    【解决方案1】:

    因为您有嵌套的定义,请尝试将内部 $().mousedown.. 等调用移出 .click() 函数。

    【讨论】:

    • 对不起,我是 jQuery 的新手:是的,如果我将它移出,它会在第一次点击时调用,但在第二次点击等时不再调用...谢谢!
    • 这可能是因为 mousemove 被调用并将 isDragging 设置为 true。使用浏览器开发者工具在这些事件上设置断点来查看这个,希望对您有所帮助
    猜你喜欢
    • 2019-09-28
    • 2011-07-13
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2021-11-22
    • 2023-03-22
    相关资源
    最近更新 更多