The mobile html5 page is a compatible solution that enables you to highlight all text content,

Source: Internet
Author: User

The mobile html5 page is a compatible solution that enables you to highlight all text content,

Recently, we need to implement a copy function for the html5 WebAPP on the page: After you click "Long press", all text is selected and the "copy" menu is displayed. You can click "copy" to copy the text, paste it to the AppStore to search for the corresponding application. Instead of directly redirecting to the App store in the form of a link, the App adds weight to the promoted enterprise App by actively entering keywords. Therefore, this "copy" function is crucial to the user experience.

I tried some practices and the compatibility on the android/iOS platform is not very good. In the browser, it is easy to activate the system menu by pressing text in a long way and highlight all the text content. However, the performance in other browsers is not very consistent. This includes simulating the focus input and JavaScript Selection, and using the tag to try to activate the system menu. These methods have compatibility defects.

1) Although the label with the href attribute is used to press the text on the uc browser and Baidu browser, the "free copy"/"select text" menu appears, after selecting this menu, the "select all/copy" menu appears. However, in some Android mobile phone system browsers and iPhones, the menu is regarded as a pure link. Only the "Copy link" is displayed ", the copy text menu does not exist. Besides, even if only a small number of browsers are considered to be feasible, this gives users more operations and increases complexity. Therefore, this solution is not advisable.

2) The selection and range methods must take into account the compatibility of different browsers. The Code is as follows:
function selectText(element) {  var doc = document,      text = doc.getElementById(element),      range,      selection;  if (doc.body.createTextRange) {      range = document.body.createTextRange();      range.moveToElementText(text);      range.select();  } else if (window.getSelection) {      selection = window.getSelection();              range = document.createRange();      range.selectNodeContents(text);      selection.removeAllRanges();      selection.addRange(range);      /*if(selection.setBaseAndExtent){          selection.setBaseAndExtent(text, 0, text, 1);      }*/  }else{      alert("none");  }}


Unfortunately, on iphone Safari, you still cannot click or press it to highlight all text (since Windows is also supported. getSelection: Why the Safari text cannot be selected. If you know the reason, please leave a message ). Therefore, this method has a defect.

3) iPhone users may know that long-pressed blank areas around the text in a text selection area, safari automatically highlights all the text in the selected area (the target text needs to be placed in an independent div block-level container ). Based on this feature, we can use CSS margin to modify it. This feature can solve the incompatibility of the second method on ios devices. After testing, Android and ios platforms are compatible with the system browsers that come with mobile phones. For mobile products of uc browser, Baidu browser, and other manufacturers, due to their different mechanisms, they can only use the "free copy" function provided by these browser menus.

Therefore, I integrated the second and third methods to use the taphold event in jquery mobile to simulate the longtap operation to stimulate the copy menu of the mobile phone system, in this way, you can easily highlight and select all text content by pressing the text area on all mobile device browsers. In another sentence, the solution is not detailed here for the compatibility bug of taphold. If your project requires perfection, you can search for a solution by yourself.

My solutions are listed below. The Code is as follows:

HTML code:

<Div class = "para requirement"> <div class = "tips-t"> 1. It must be downloaded for the first time. <br/> 2. It cannot be downloaded from the ranking table. </ div> <div class = "cparea"> <div class = "kwd" id = "kwd"> <span> mobile optimization master of Three Kingdoms Yanyi </span> </div> </div> <div class = "cparea"> <span class = "kdes"> <B>★</B> long-pressed dotted box, copy keywords </span> </div> <a href = "https://itunes.apple.com/cn/" data-role = "button" class = "downlink"> Search and download from AppStore </a> </ div>

JavaScript code:

<Script type = "text/javascript"> $ ("# kwd "). bind ("taphold", function () {// iPhone/iTouch/iPad Safari var doc = document, text = doc is not supported. getElementById ("kwd"), range, selection; if (doc. body. createTextRange) {range = document. body. createTextRange (); range. moveToElementText (text); range. select ();} else if (window. getSelection) {selection = window. getSelection (); range = document. createRange (); range. selectNodeContents (text); selection. removeAllRanges (); selection. addRange (range);} else {alert ("the browser does not support the long-press copy function") ;}}); </script>

Key CSS code:

.cparea{text-align: center;font-family: Microsoft Yahei;margin: -2em 0 0;}.kwd{display: inline-block;color: #272727;background-color: #fff;font-size: 1.1875em;font-size: 1.1875em;padding: .75em 1em;border: 1px dashed #e60012;-webkit-user-select:element; margin: 2em;}.kwd span{display: block; border: 1px solid #fff;}.kdes{display: inline-block;color: #212121;font-size: .875em;padding-top: 0;}.kdes b{color: #ed5353;font-size: 1.25em;padding-right: .1em;}

Note: here the margin: 2em is designed to achieve the long-press-all function on the Safari browser, in order to respect the effect of restoring the design draft, the parent container. cparea uses a negative margin to offset the 2em margin. In the end, not only is the visual and design drawings consistent, but also enables the system menu to be fully selected by a long press.


For more information, see the CSDN freshlover space.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.