5

我正在使用与 jQuery Mobile 1.1.1 捆绑的 Phonegap 为 iOS 开发一个应用程序。我的页面上有一个 div 正在监听点击和点击事件。

我面临的问题是,一旦我抬起手指,就会在点击事件之后触发点击事件。我该如何防止这种情况?这里提供了一个解决方案,但这是唯一的方法吗?如果您需要使用布尔标志来区分两者,则有点取消了为 tap 和 taphold 设置两个不同事件的全部意义。

以下是我的代码:

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

    $('#divOne').bind('tap', function () {
      console.log("TAPPED!!");
    });
});

非常感谢您的帮助。谢谢!

4

7 回答 7

3

只需将其设置在文档顶部或定义偶数之前的任何位置:

$.event.special.tap.emitTapOnTaphold = false;

然后你可以像这样使用它:

$('#button').on('tap',function(){
    console.log('tap!');
}).on('taphold',function(){
    console.log('taphold!');
});
于 2014-02-18T08:54:18.813 回答
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 );
        });
    }
};
于 2012-08-13T20:18:23.037 回答
0

您可以使用 jquery 的 stopImmediatePropagation() 方法来解决此问题。根据jquery api中的解释, stopImmediatePropagation() 方法

“阻止其余的处理程序被执行,并防止事件在 DOM 树中冒泡。”

于 2012-08-01T13:13:34.793 回答
0

由于滑动,触发点击然后我能够保持简单:

    $(c).bind("taphold",function(e){
        if(e.target.wait){
            e.target.wait = false;
        }else{
            alert("fire the taphold");
        }//eo if not waiting
    });
    $(c).bind("swipe",function(e){
            e.target.wait = true;//taphold will come next if I don't wave it off
        alert(e.target.text+"("+e.target.attributes.dataId.value+") got swiped");
        return false;
    });

为了也支持点击,我会推迟等待清除,直到点击事件也将始终触发。

于 2014-06-14T21:46:45.443 回答
0

把它放在你的 taphold 事件处理程序中......这个建议假设 o 是一个触发 taphold 的 jQuery 对象

jQuery(o).one('点击点击', function(){ return false; });

绑定到一种方法只会触发一次事件。如果它是 <a> 标签,则返回 false 将停止该事件的执行。

于 2013-06-14T04:22:36.420 回答
0

我仍然有问题,使用jquery-mobile的taphold,我解决了在taphold之后调用的点击问题,在元素上设置了超时。JQM 1.4 与 emitTapOnTaphold = false;

例子:

$(".element").on("taphold", function () {
        // function her

         setTimeout (function () {
             $(this).blur();
         400);
});
于 2018-01-12T17:14:14.787 回答
-1

$.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 );
    });
}

};

@Akash Budhia: Thanks for your solutions. It's great, sounds it work for me!

于 2015-02-09T01:31:09.933 回答