Home Web Front-end HTML Tutorial How does JavaScript keep web page selections still highlighted in blue after losing focus?

How does JavaScript keep web page selections still highlighted in blue after losing focus?

Apr 05, 2025 am 06:30 AM
Browser Solution

How does JavaScript keep web page selections still highlighted in blue after losing focus?

JavaScript web page selection highlighting skills

In web interaction, when the user selects text, the browser is usually highlighted in blue. However, when the page loses focus, the highlight may disappear and turn gray. This article introduces how to use JavaScript code to keep the selection highlighted in blue after the page loses focus.

Problem: The user selects text (for example, <textarea></textarea>元素内),选区高亮显示为蓝色。但点击页面其他区域,页面失去焦点后,高亮颜色变灰。如何用JavaScript代码保持蓝色高亮?

解决方案: 浏览器没有直接修改选区颜色属性的API。解决方法是保存并恢复选区信息。在页面失去焦点前保存选区信息,需要时再恢复。

以下代码片段演示如何通过保存和恢复Range对象来实现:

let lastRange = null; const txt = document.getElementById('myTextbox'); // 将'myTextbox'替换为你的文本框ID txt.onkeyup = function(e) { const selection = window.getSelection(); lastRange = selection.rangeCount > 0 ? selection.getRangeAt(0) : null; }; const btn = document.getElementById('restoreButton'); // 将'restoreButton'替换为你的按钮ID btn.onclick = () => { const selection = window.getSelection(); selection.removeAllRanges(); if (lastRange) { selection.addRange(lastRange); } };
Copy after login

这段代码在onkeyup事件中,使用window.getSelection()获取当前选区,并用getRangeAt(0)获取第一个Range对象,保存到lastRange变量。 点击按钮(btn)时,代码清除所有选区,然后使用selection.addRange(lastRange)重新添加保存的Range对象,恢复之前的选区。

重要说明: 这并非直接改变选区颜色,而是通过重建选区来间接恢复高亮显示。选区颜色由浏览器决定,JavaScript无法直接控制。 此方法的本质是重新创建选区,而非修改颜色。 请确保你的HTML中包含一个ID为myTextbox的文本框和一个ID为restoreButton的按钮。

The above is the detailed content of How does JavaScript keep web page selections still highlighted in blue after losing focus?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to solve the problem of printing spaces in IDEA console logs? How to solve the problem of printing spaces in IDEA console logs? Apr 19, 2025 pm 09:57 PM

How to solve the problem of printing spaces in IDEA console logs? When using IDEA for development, many developers may encounter a problem: the console printed...

Why can't JavaScript directly obtain hardware information on the user's computer? Why can't JavaScript directly obtain hardware information on the user's computer? Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

How to parse next-auth generated JWT token in Java and get information in it? How to parse next-auth generated JWT token in Java and get information in it? Apr 19, 2025 pm 08:21 PM

In processing next-auth generated JWT...

What is the reason why the results of JSONObject and Map serialization are inconsistent? How to solve it? What is the reason why the results of JSONObject and Map serialization are inconsistent? How to solve it? Apr 19, 2025 pm 10:21 PM

Discussing the reasons and solutions for inconsistent results of JSONObject and Map serialization. When serializing data, we often use different data structures to...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Can JWT implement dynamic permission changes? What is the difference from the Session mechanism? Can JWT implement dynamic permission changes? What is the difference from the Session mechanism? Apr 19, 2025 pm 06:12 PM

Confusion and answers about JWT and Session Many beginners are often confused about their nature and applicable scenarios when learning JWT and Session. This article will revolve around J...

See all articles