


Mobile terminal implements selected and highlighted all-selected text events
This time I will bring you the event of selecting and highlighting all selected text on the mobile terminal. What are the precautions on implementing the event of selecting and highlighting all selected text on the mobile terminal. The following is a practical case, let's take a look.
Recently, we need to implement a copy function on the page for HTML5 WebAPP: when the user clicks and long-presses the text, all the text will be selected and the system "Copy" menu will pop up. The user can click "Copy" to perform the copy operation, and then paste it into Search the AppStore for the corresponding application. The reason why the link form is not used to jump directly to the corresponding AppStore application is to increase the weight of the promoted enterprise App through the user's active input of keyword searches. Therefore, this "copy" function is crucial to the user experience.
I tried some methods, but the compatibility on the Android/iOS platform is not very good. In the WeChat browser, it is easy to long-press text to activate the system menu and highlight all selected text content. But the performance in other browsers is not very consistent. Including simulating focus input, JavaScript Selection, and using the a tag to try to activate the system menu. These methods all have compatibility flaws.
1) Although using the a tag with href attribute and long pressing the text on uc browser and Baidu browser, the "free copy"/"select text" menu will appear. After selecting this menu, the "full text" will appear. "Select/Copy" menu, but in the system browsers of some Android phones and iPhones, it is regarded as a pure link, and only "Copy Link" pops up without the "Copy Text" menu. Moreover, even if only a small number of browsers are considered feasible, this will add one more step to the user's operation and increase complexity. So this solution is not advisable.
2) Using selection and range methods, you need to consider the compatibility of different browsers. The code is as follows:
function selectText(element) { var doc = document, text = docgetElementById(element), range, selection; if (docbodycreateTextRange) { range = documentbodycreateTextRange(); rangemoveToElementText(text); rangeselect(); } else if (windowgetSelection) { selection = windowgetSelection(); range = documentcreateRange(); rangeselectNodeContents(text); selectionremoveAllRanges(); selectionaddRange(range); /*if(selectionsetBaseAndExtent){ selectionsetBaseAndExtent(text, 0, text, 1); }*/ }else{ alert("none"); } }
Unfortunately, it is still not possible to highlight by clicking or long pressing on iPhone Safari Select all text (since window.getSelection is also supported, why can’t the text be selected by default after the addRange operation in Safari browser? If you know the reason, please leave a message). Therefore, this approach is flawed. The method of actively selecting a text area is attached later.
3) iPhone users may know that if you long press the blank area around the text in a text selection, Safari will automatically highlight and select all the text in the selection (the target text needs to be placed in an independent p block-level container ). According to this feature, use CSS margin to modify it. Using this feature, you can solve the incompatibility of the second method above on iOS devices. After testing, regardless of the Android and iOS platforms, the system browsers that come with general mobile phones are compatible. As for mobile products from other manufacturers such as UC Browser and Baidu Browser, due to different mechanisms, you can only use the "free copy" function provided by these browser menus.
So, I combined the second and third methods and used the taphold event in jQuery mobile to simulate the longtap operation to trigger the copy menu of the mobile system. This can basically be done on all mobile device browsers. You can long press the text area to highlight all text content. One more thing, taphold's compatibility bugs are not detailed here with solutions. If your project requires excellence, you can search for solutions yourself.
My solution is listed below. The specific code is as follows:
HTML code:
<p class=" para requirement"> <p class="tips tips-t"> 1、必须首次下载才生效<br/> 2、不能从排行榜下载哦 </p> <p class="cparea"> <p class="kwd" id="kwd"><span>三国艳义手机优化大师</span></p> </p> <p class="cparea"> <span class="kdes"><b>★</b>长按虚线框,拷贝关键词</span> </p> <a href="https://itunesapplecom/cn/" data-role="button" class="downlink">去AppStore搜索下载</a> </p>
JavaScript code:
<script type="text/javascript"> $("#kwd")bind("taphold", function(){ //不支持iPhone/iTouch/iPad Safari var doc = document, text = docgetElementById("kwd"), range, selection; if (docbodycreateTextRange) { range = documentbodycreateTextRange(); rangemoveToElementText(text); rangeselect(); } else if (windowgetSelection) { selection = windowgetSelection(); range = documentcreateRange(); rangeselectNodeContents(text); selectionremoveAllRanges(); selectionaddRange(range); }else{ alert("浏览器不支持长按复制功能"); } }); </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: 1875em; font-size: 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: 25em; padding-right: 1em; }
Description: The margin here: 2em is correct In order to realize the long-press selection all function on Safari browser, in order to respect the effect of restoring the design draft, the parent container .cparea uses negative margins to offset the 2em margin. In the end, not only was it visually consistent with the design drawing, but it also enabled long-pressing to select all to activate the system menu.
Finally, let me add the complete method to support Safari:
$("#kwd").bind("taphold", function(){ var doc = document, text = docgetElementById("kwd"), range, selection; if (docbodycreateTextRange) { //IE range = documentbodycreateTextRange(); rangemoveToElementText(text); rangeselect(); } else if (windowgetSelection) { //FF CH SF selection = windowgetSelection(); range = documentcreateRange(); rangeselectNodeContents(text); selectionremoveAllRanges(); selectionaddRange(range); //测试 consolelog(texttextContent); textinnerText && consolelog(textinnerText); //FireFox不支持innerText consolelog(texttextContentlength); textinnerText && consolelog(textinnerTextlength); //在Chrome下长度比IE/FF下多1 consolelog(textfirstChildtextContentlength); textinnerText && consolelog(textfirstChildinnerTextlength); consolelog(textfirstChildinnerHTMLlength); //注意IE9-不支持textContent makeSelection(0, textfirstChildtextContentlength, 0, textfirstChild); /* if(selectionsetBaseAndExtent){ selectionselectAllChildren(text); selectionsetBaseAndExtent(text, 0, text, 4); } */ }else{ alert("浏览器不支持长按复制功能"); } }); function makeSelection(start, end, child, parent) { var range = documentcreateRange(); //consolelog(parentchildNodes[child]); rangesetStart(parentchildNodes[child], start); rangesetEnd(parentchildNodes[child], end); var sel = windowgetSelection(); selremoveAllRanges(); seladdRange(range); }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the use of date-related functions in JS
Detailed explanation of the use of foldable panels in JS
The above is the detailed content of Mobile terminal implements selected and highlighted all-selected text events. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Realizing love animation effects through Java code In the field of programming, animation effects are very common and popular. Various animation effects can be achieved through Java code, one of which is the heart animation effect. This article will introduce how to use Java code to achieve this effect and give specific code examples. The key to realizing the heart animation effect is to draw the heart-shaped pattern and achieve the animation effect by changing the position and color of the heart shape. Here is the code for a simple example: importjavax.swing.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.
