taphold jQuery Mobile 1.1.1后点击事件

are*_*s05 5 events jquery-mobile cordova

我正在使用与jQuery Mobile 1.1.1捆绑在一起的Phonegap为iOS开发应用程序.我的页面上有一个div,它正在监听tap和taphold事件.

我面临的问题是,一旦我抬起手指,敲击事件就会触发敲击事件.我该如何防止这种情况?这里提供一个解决方案,但这是唯一的方法吗?如果您需要使用布尔标志来区分这两个事件,Kinda会为tap和taphold设置两个不同的事件.

以下是我的代码:

$('#pageOne').live('pageshow', function(event) {
    $('#divOne').bind('taphold', function (event) {
       console.log("TAP HOLD!!");    
    });

    $('#divOne').bind('tap', function () {
      console.log("TAPPED!!");
    });
});
Run Code Online (Sandbox Code Playgroud)

非常感谢帮助.谢谢!

Aka*_*hia 2

[尝试和测试] 我检查了 jQuery Mobile 的实现。每次在“vmouseup”上,他们都会在“taphold”之后触发“tap”事件。

解决方法是如果“taphold”已被触发,则不触发“tap”事件。创建自定义事件或根据需要修改源,如下所示:

$.event.special.tap = {
    tapholdThreshold: 750,

    setup: function() {
        var thisObject = this,
            $this = $( thisObject );

        $this.bind( "vmousedown", function( event ) {

            if ( event.which && event.which !== 1 ) {
                return false;
            }

            var origTarget = event.target,
                origEvent = event.originalEvent,
                /****************Modified Here**************************/
                tapfired = false,
                timer;

            function clearTapTimer() {
                clearTimeout( timer );
            }

            function clearTapHandlers() {
                clearTapTimer();

                $this.unbind( "vclick", clickHandler )
                    .unbind( "vmouseup", clearTapTimer );
                $( document ).unbind( "vmousecancel", clearTapHandlers );
            }

            function clickHandler( event ) {
                clearTapHandlers();

                // ONLY trigger a 'tap' event if the start target is
                // the same as the stop target.
                /****************Modified Here**************************/
                //if ( origTarget === event.target) {
                 if ( origTarget === event.target && !tapfired) {
                     triggerCustomEvent( thisObject, "tap", event );
                 }
            }

            $this.bind( "vmouseup", clearTapTimer )
                .bind( "vclick", clickHandler );
            $( document ).bind( "vmousecancel", clearTapHandlers );

            timer = setTimeout( function() {
                tapfired = true;/****************Modified Here**************************/
                triggerCustomEvent( thisObject, "taphold", $.Event( "taphold", { target: origTarget } ) );
            }, $.event.special.tap.tapholdThreshold );
        });
    }
};
Run Code Online (Sandbox Code Playgroud)