jQuery jquery中的ontouchstart和ontouchend?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12339923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:35:09  来源:igfitidea点击:

ontouchstart and ontouchend in jquery?

jquerycsshtmltouch

提问by Olokoo

I am currently using the following for every element I want to change the class of on touch:

我目前正在为每个要更改触摸类的元素使用以下内容:

ontouchstart="$(this).addClass('select');" ontouchend="$(this).removeClass('select');"

I was wondering if there is something like this?:

我想知道是否有这样的事情?:

$("#element").touchstart(function(){
    $(this).addClass('select');
},
function(){
   $(this).removeClass('select');
});

That I would be able to list all the elements that I want to have this property. I have tried so many things and cant get it to work.

我将能够列出我想要拥有此属性的所有元素。我已经尝试了很多东西,但无法让它发挥作用。

回答by Olokoo

I ended up just using this:

我最终只使用了这个:

$('#nav li').bind('touchstart', function(){
    $(this).addClass('select');
}).bind('touchend', function(){
    $(this).removeClass('select');
});

回答by Reign.85

Without JQUERY :

没有 JQUERY :

element.addEventListener('touchstart',function(e) {
 e.currentTarget.className = "select";
});

With JQUERY

使用 JQUERY

$('#nav ul li a').on('touchstart', function(){
    $(this).addClass('select');
});

Actually, on() get better performance than bind(), check documentation

实际上,on() 的性能比 bind() 更好,请查看文档

回答by Chris

I think you need to use jQuery Mobile. It has some normalized events, which are quite possibly what you need. Here's a list of special events from jQuery Mobile's API reference: little link.

我认为您需要使用jQuery Mobile。它有一些规范化的事件,这很可能是您所需要的。以下是 jQuery Mobile 的 API 参考中的特殊事件列表:小链接

You care about those:

你关心的是:

vmousedown

Normalized event for handling touchstart or mousedown events

vmousemove

Normalized event for handling touchmove or mousemove events

vmouseup

Normalized event for handling touchend or mouseup events

鼠标按下

Normalized event for handling touchstart or mousedown events

鼠标移动

Normalized event for handling touchmove or mousemove events

鼠标

Normalized event for handling touchend or mouseup events

回答by WeP

Why don't you use the :hoverpseudo-class for this? It seems to me that this is a temporary change of the CSS rules of the nav_lielement.

为什么不:hover为此使用伪类?在我看来,这是nav_li元素CSS 规则的临时更改。