Javascript 在 iPhone 上的移动 Safari 中选择文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1899421/
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-22 21:48:48  来源:igfitidea点击:

Selecting text in mobile Safari on iPhone

javascriptiphonecopymobile-safarizeroclipboard

提问by Goldy

I'm trying to make it easy for an iphone user to copy some text to the clipboard in mobile safari. As in the usual "touch-hold-copy". There is a specific bit of text I want to a user to copy. I have full choice of the html markup in which to wrap the text. How can I make it easy, rather than abitrary? For instance:

我试图让 iphone 用户轻松地将一些文本复制到移动 safari 中的剪贴板。就像通常的“触摸保持复制”一样。我希望用户复制一段特定的文本。我可以完全选择用于包装文本的 html 标记。我怎样才能让它变得容易,而不是随意?例如:

  • Is there a way to "select all" the text upon touch-down using javascript? Then a user could just continue to touch-hold and then choose copy?

  • Is there a way to bring up the "select all" option? Like you can when typing in a text box? After which they can choose copy?

  • If there's no javascript solution, how can I arrange the html to help Safari select the right bit of text easily? As opposed to just a word, or a wrapping div?

  • 有没有办法在使用 javascript 触地时“全选”文本?那么用户可以继续按住然后选择复制吗?

  • 有没有办法调出“全选”选项?像在文本框中键入时一样吗?之后他们可以选择复制吗?

  • 如果没有 javascript 解决方案,我如何安排 html 以帮助 Safari 轻松选择正确的文本位?与只是一个词或一个包装 div 相对?

I've tried onFocus="this.select()" for various elements, none seem to work. Also tried onClick.

我已经为各种元素尝试过 onFocus="this.select()" ,但似乎都不起作用。也试过onClick。

Those who have tried to port a site that uses ZeroClipboard to the iPhone might have some ideas.

那些尝试将使用 ZeroClipboard 的网站移植到 iPhone 的人可能会有一些想法。

Cheers

干杯

回答by avshalom

instead of this.select();I used the following and it worked!

而不是this.select();我使用了以下内容并且它起作用了!

this.selectionStart=0;
this.selectionEnd=this.value.length;

回答by Streeter

The magic sauce for me was the combination of these three:

对我来说神奇的酱汁是这三个的组合:

onFocus="this.selectionStart=0; this.selectionEnd=this.value.length;" <!-- for big screens -->

onTouchEnd="this.selectionStart=0; this.selectionEnd=this.value.length;" <!-- for small screens -->

onMouseUp="return false" <!-- to stop the jitters -->

回答by jimmy javascript

Try ontouchstart instead of onfocus. Onfocus fires approx. 500ms after ontouchend, same as onclick, onmousedown, and onmouseup. See https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW7for more details on mouse events.

尝试 ontouchstart 而不是 onfocus。Onfocus 火大约。ontouchend 后 500 毫秒,与 onclick、onmousedown 和 onmouseup 相同。有关鼠标事件的更多详细信息,请参阅https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW7

回答by Brien Malone

I have run into the same problem. The onfocus event is the right one to trap (ontouchstart isn't triggered if you use the iphone keyboard [next]/[prev] buttons.) If you put an alert(); in your onfocus="" handler, you'll see the alert box pop up. The problem is this.select(); I still haven't found an answer to this, but when/if I do, I'll post it here.

我遇到了同样的问题。onfocus 事件是正确的陷阱(如果您使用 iPhone 键盘 [next]/[prev] 按钮,则不会触发 ontouchstart。)如果您放置了 alert(); 在您的 onfocus="" 处理程序中,您会看到警告框弹出。问题是 this.select(); 我仍然没有找到答案,但是当/如果我找到了,我会在这里发布。