Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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 使函数看起来像事件一样友好_Javascript_Jquery - Fatal编程技术网

Javascript 使函数看起来像事件一样友好

Javascript 使函数看起来像事件一样友好,javascript,jquery,Javascript,Jquery,此代码适用于: $('.test').swipe({ tap: function(){ alert('testing'); } }); $('#hello').swipe({ tap: function(){ alert('hello world'); } }); 我可以通过以下方式继续: 然后,要使用它,我可以做: tap('.test', function(){ alert('testing'); }); tap(

此代码适用于:

$('.test').swipe({
    tap: function(){
        alert('testing');
    }
});

$('#hello').swipe({
    tap: function(){
        alert('hello world');
    }
});
我可以通过以下方式继续:

然后,要使用它,我可以做:

tap('.test', function(){
    alert('testing');
});

tap('#hello', function(){
    alert('hello world');
});
但我希望它看起来更友好,像一个活动:

$('.test').on('tap', function(){
    alert('testing');
});

$('#hello').on('tap', function(){
    alert('hello world');
});
我如何才能以这种方式恢复它?

,您可以创建自己的事件。由于您不想使用jQuery移动库,因此可以创建自己的
点击
事件。例如:

$('.test').on('tap', function(){
    alert('testing');
});

$('#hello').on('tap', function(){
    alert('hello world');
});

$('#bye').on('tap', function(){
    alert('leaving out');
});
然后触发事件:

$('.test').swipe({
    tap: function(){
        $(this).trigger('tap')
    }
});
$('#hello').swipe({
    tap: function(){
        $(this).trigger('tap')
    }
});

$('#bye').swipe({
    tap: function(){
        $(this).trigger('tap')
    }
});

您可以用纯javascript或jquery创建自己的事件。看看这个:| |这也很有趣。不过,您不想使用第一个符号来明确区分在给定DOM元素上运行的小部件和小部件事件吗?(在这方面,
swipe
小部件应该在
$('.test')
上创建
tap
事件,但符号将保持不变,我猜)您正在使用jquery mobile吗?如果是这样,它看起来像是有一个点击事件。忘记添加链接了@艾瑟多格。这是因为我将使用更频繁的
点击
。。。第一种方法总是可能的:)@我正在调查MarcosPérezGude。。谢谢@t34t5不,我没有,因为我正在使用这个
swipe
库,我更喜欢这样做,而不是添加另一个库。我的jquery没有打开
('swipe')
。。我使用的是
touchwipe.js
,如果可能的话,我想避免仅仅因为这个原因而添加jQuery移动库。我认为问题更多的是通过触发事件触发函数(由小部件创建)——在这方面,
tap
的触发器应该运行所有的
$。fn[widget]tap
函数;至少我是这样读的。我想这一切都取决于小部件的实际功能——是否绑定事件,或者not@WashingtonGuedes我编辑了我的答案来反映这一点。我很困惑。。。我已经编辑了我的问题。。你能展示一下我的新测试用例是怎样的吗?你能把
$('#hello')
事件添加到你的答案中吗?也许我知道会是什么样子
$('.test').swipe({
    tap: function(){
        $(this).trigger('tap')
    }
});
$('#hello').swipe({
    tap: function(){
        $(this).trigger('tap')
    }
});

$('#bye').swipe({
    tap: function(){
        $(this).trigger('tap')
    }
});