简体   繁体   English

模拟Java中的长按

[英]Simulate Long Press in Javascript

I have a webapp where when the user clicks on a field, the text inside is highlighted for him to copy. 我有一个webapp,当用户单击某个字段时,将突出显示其中的文本供他复制。 However, on Android this does not trigger the opening of the copy context menu, so the user must select the text himself. 但是,在Android上,这不会触发打开复制上下文菜单,因此用户必须自己选择文本。

Is there a way to programmatically trigger the long press event so that the copy/paste context menu appears on mobile browsers? 是否可以通过编程方式触发长按事件,以便复制/粘贴上下文菜单出现在移动浏览器中?

Maybe you can achieve this by using the taphold event from jquery mobile. 也许您可以通过使用jquery mobile中的taphold事件来实现此目的。

http://api.jquerymobile.com/taphold/ http://api.jquerymobile.com/taphold/

I know it isn't exactly the solution that you've been looking for, but it is a solution that worked for me in many web apps. 我知道这并不是您一直在寻找的解决方案,但它对许多Web应用程序都有效。 Instead of letting the user copy/paste it by himself, I'm adding a copy button. 我没有让用户自己复制/粘贴它,而是添加了复制按钮。 For the most part, I believe this results in better user experience. 在大多数情况下,我相信这会带来更好的用户体验。

There are a few libraries that do exactly that with a very small footprint that does not rely on Adobe Flash to do so. 有一些库可以在不依赖Adobe Flash的情况下以很小的占用空间来完全做到这一点。

I've been using clipboard.js for a while, it works great on mobile as well. 我使用剪贴板有一段时间了,它在移动设备上也很好用。

From ecma6 javascript, we can use GlobalEventHandlers , to detect keys and touch events. 通过ecma6 javascript,我们可以使用GlobalEventHandlers来检测按键和触摸事件。 There is a lot of different handlers for different events. 对于不同的事件有很多不同的处理程序。

When the user touch/key/click an element, we can detect it in many ways, but for your exact query, a touch/click event is made of two different actions: ontouchstart and ontouchend . 当用户触摸/按键/单击元素时,我们可以通过多种方式对其进行检测,但是对于您的确切查询,触摸/单击事件由两个不同的动作组成: ontouchstartontouchend

It basically means that when ontouchend isn't triggered, the user is holding the element by touching, this is a long touch/click. 从根本上讲 ,这意味着在未触发ontouchend时,用户通过触摸来按住元素,这是长时间的触摸/点击。

The following example use onmouseover , onmousleave , ontouchstart and ontouchend events. 以下示例使用onmouseoveronmousleaveontouchstartontouchend事件。

 shot.onmouseover = (function(){ console.log("Mouse action started!") }) shot.onmouseleave = (function(){ console.log("Mouse action terminated!") }) shot.ontouchstart = (function(){ console.log("Touch action started!") }) shot.ontouchend = (function(){ console.log("Touch action terminated!") }) 
 #shot {width:100%;min-height:300px;background:red} 
 <div id="shot">Touch </div> 

The following example emulates Android Long press. 以下示例模拟了Android长按。 Put your action after the long press inside the setTimeout: 在setTimeout内长按之后,将您的操作放入:

  var timer; //Time of the long press const tempo = 1000; //Time 1000ms = 1s const mouseDown = () => { timer = setTimeout(function(){ //Insert your function here alert("Your Function Here!"); }, tempo); }; const mouseUp = () => { clearTimeout(timer); }; 
 <p ontouchstart="mouseDown()" ontouchend="mouseUp()" onmousedown="mouseDown()" onmouseup="mouseUp()">Long Touch Me!</p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM