动态突出显示(更改背景)/网页中的内容

Highlight (change background) / of the content in a web page dynamically

本文关键字:网页 背景 显示 动态      更新时间:2023-09-26

有没有办法在选择特定文本时动态更改网页中文本background-color

前任:

示例文本显示在 html 页面中。我想突出显示该页面中的特定文本,同时从鼠标指针中进行选择。

我的基础工作 :

为特定的选定文本添加类"CSS 样式"。但是如何动态地从网页中获取所选文本。

您可以使用

CSS 选择器::selection动态设置所选文本的样式。

#content::selection {
  background-color: plum;
  color: white;
}
#content::-moz-selection {
  background-color: plum;
  color: white;
}
<div id="content">Sample text display in the html page. I want to high light the particular text in that page, while selecting from mouse pointer.</div>