Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用修改的扩展jQuery。单击函数_Javascript_Jquery_Touch - Fatal编程技术网

Javascript 使用修改的扩展jQuery。单击函数

Javascript 使用修改的扩展jQuery。单击函数,javascript,jquery,touch,Javascript,Jquery,Touch,我正在为移动/触摸设备以及桌面非触摸设备创建一个站点。 为了帮助消除jQuery上的300毫秒延迟,我使用的是tappy库,它工作得很好 https://github.com/filamentgroup/tappy 也就是说,我只想在必要时绑定此“点击”事件,并想绑定一个常规事件。如果设备是非触摸式的,请单击事件。我知道tappy的“tap”事件将针对非触摸设备启动,但我宁愿只在必要时使用它,因为有一些怪癖被记录在案 到目前为止,每次我想这样做时,我都在编写开关函数,如下所示: if ($('

我正在为移动/触摸设备以及桌面非触摸设备创建一个站点。 为了帮助消除jQuery上的300毫秒延迟,我使用的是tappy库,它工作得很好

https://github.com/filamentgroup/tappy
也就是说,我只想在必要时绑定此“点击”事件,并想绑定一个常规事件。如果设备是非触摸式的,请单击事件。我知道tappy的“tap”事件将针对非触摸设备启动,但我宁愿只在必要时使用它,因为有一些怪癖被记录在案

到目前为止,每次我想这样做时,我都在编写开关函数,如下所示:

if ($('html').hasClass('no-touch')) {
    $('.sidebar').find('.strip').find('.explore-wrapper').click(function() {
      $(this).closest('.sidebar').addClass('active');
    });
} else {    
    $('.sidebar').find('.strip').find('.explore-wrapper').bind('tap', function() {
      $(this).closest('.sidebar').addClass('active');    
    });
 }
我想扩展jQuery并编写我自己的版本。单击,可能称为“tapClick”,这样我的代码可以像这样运行:

$('.sidebar').find('.strip').find('.explore-wrapper').tapClick(function() {
    $(this).closest('.sidebar').addClass('active');
});
我开始尝试编写自己的扩展,但无法获得它…任何帮助都将不胜感激:

// Custom tap/click bind
$.extend({
    tapClick : function(callbackFnk) {
        if ($('html').hasClass('touch')) {
            $(this).bind('tap', function() {
                if(typeof callbackFnk == 'function') {
                    callbackFnk.call(this);
                }
            });
        } else {
            $(this).click(function() {
                if(typeof callbackFnk == 'function') {
                    callbackFnk.call(this);
                }
            });
        }
    }
});
试着代替

e、 g

$.extend({tapClick:function(){console.log(this)}});
$.fn.extend({tapClick:function(){console.log(this)}});
$.tapClick();
$(“html”).tapClick()

这是我最后用来解决问题的代码

$.fn.extend({
    tapClick:function(callback) {
        if ($('html').hasClass('touch')) {
            return $(this).click(callback);
        } else {
            return $(this).bind('tap', callback);
        }
    }
});

也许这会很有帮助,谢谢。我最终接受了你的代码并修改它以满足我的需要,现在它工作得很好。关键是使用“this”并将其传递回。您的答案已显示在低质量帖子审阅队列中,因为您没有包含任何代码解释。通常,在解释代码时,它会改进答案。
$.fn.extend({
    tapClick:function(callback) {
        if ($('html').hasClass('touch')) {
            return $(this).click(callback);
        } else {
            return $(this).bind('tap', callback);
        }
    }
});