简体   繁体   English

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

[英]Selecting text in mobile Safari on iPhone

I'm trying to make it easy for an iphone user to copy some text to the clipboard in mobile safari. 我试图让iphone用户轻松地将一些文本复制到移动版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. 我可以完全选择用于包装文本的html标记。 How can I make it easy, rather than abitrary? 我怎样才能轻松而不是抄袭? For instance: 例如:

  • Is there a way to "select all" the text upon touch-down using javascript? 有没有办法在使用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? 如果没有javascript解决方案,我如何安排html以帮助Safari轻松选择正确的文本位? As opposed to just a word, or a wrapping div? 与单词或包装div相反?

I've tried onFocus="this.select()" for various elements, none seem to work. 我已经尝试过onFocus =“this.select()”用于各种元素,似乎都没有用。 Also tried onClick. 也试过onClick。

Those who have tried to port a site that uses ZeroClipboard to the iPhone might have some ideas. 那些试图将使用ZeroClipboard的网站移植到iPhone上的人可能会有一些想法。

Cheers 干杯

instead of this.select(); 而不是this.select(); I used the following and it worked! 我使用了以下内容并且有效!

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

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 -->

Try ontouchstart instead of onfocus. 尝试ontouchstart而不是onfocus。 Onfocus fires approx. Onfocus发射约。 500ms after ontouchend, same as onclick, onmousedown, and onmouseup. 在ontouchend之后500ms,与onclick,onmousedown和onmouseup相同。 See https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW7 for more details on mouse events. 有关鼠标事件的更多详细信息,请参阅https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW7

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(); 陷阱事件是正确的陷阱事件(如果您使用iphone键盘[next] / [prev]按钮,则不会触发ontouchstart。)如果您发出警报(); in your onfocus="" handler, you'll see the alert box pop up. 在你的onfocus =“”处理程序中,你会看到弹出警告框。 The problem is this.select(); 问题是this.select(); I still haven't found an answer to this, but when/if I do, I'll post it here. 我还没有找到答案,但是如果我这样做,我会在这里发布。

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

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