Selecting text in mobile Safari on iPhone

前端 未结 4 608
闹比i
闹比i 2020-12-13 02:41

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 wa

相关标签:
4条回答
  • 2020-12-13 03:17

    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-SW7 for more details on mouse events.

    0 讨论(0)
  • 2020-12-13 03:24

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

    this.selectionStart=0;
    this.selectionEnd=this.value.length;
    
    0 讨论(0)
  • 2020-12-13 03:29

    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 -->
    
    0 讨论(0)
  • 2020-12-13 03:30

    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.

    0 讨论(0)
提交回复
热议问题