gpt4 book ai didi

javascript - 让函数看起来像一个事件一样友好

转载 作者:行者123 更新时间:2023-12-01 01:42:45 26 4
gpt4 key购买 nike

此代码有效:

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

$('#hello').swipe({
tap: function(){
alert('hello world');
}
});

我可以恢复它:

function tap(obj, fn){
$(obj).swipe({
tap: fn
});
};

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

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

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

但我希望它像事件一样看起来更友好:

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

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

我怎样才能恢复这种方式?

最佳答案

As Marcos Pérez Gude said ,您可以创建自己的事件。由于您不想使用 jQuery 移动库,因此您可以创建自己的 tap 事件。例如:

$('.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 - 让函数看起来像一个事件一样友好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34795207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com