javascript - 在 html 中标记文本 (JS/Vue.js)
问题描述
如何突出显示html中的文本?我有一篇现成的 html 文章,我想在那里标记文本。
我正在努力:
search= ""
let words = el.text.split(' ')
for (let key in words) {
if (words[key].includes(this.search)) {
words[key] = "<mark>" + words[key] + "</mark>";
}
}
el.text = n.join(" ");
突出显示纯文本效果很好:
该网站由 Jeff Atwood 和 Joel Spolsky 于 2008 年创建。
但是复杂的标记和 html 标签呢:
<p><b>Stack Overflow</b> is a question and answer site for professional and enthusiast programmers. It is a privately held website, the flagship site of the <a href="/wiki/Stack_Exchange" title="Stack Exchange">Stack Exchange Network</a>
如何拆分为数组元素:文本:<h1>Stack Overflow<h1> is a question and <p>answer site for professional and enthusiast programmers.</p>
如果我们搜索“堆栈” - 将被标记<mark><h1>Stack</mark>
解决方案
推荐阅读
- java - 为什么 onClick() 函数需要参数而不是参数
- sql - 在 SAS Data Integration Studio 中将两个或多个字段的数据合并为一个字段
- python - 就数据而言,我的神经网络模型如何找到(从我的数据集中)最接近用户输入列表的列表?
- android-studio - 启用虚拟化后 Android Studio 无法启动
- sql-server - 在复杂字段上连接两个表
- powershell - 使用 PowerShell 使用 multipart/form-data 将文件上传到 API 端点
- c++ - 如何绘制在 C++ 中使用 gsl_multifit_linear 函数找到的最佳拟合曲线?
- string - 什么是一种惯用的 Scala 方式来连接字符串并首先删除特定的重复元素
- firebase - Firebase 函数不会看到 pubsub 模拟器在本地运行
- flutter - 如何布局 2 行,每行占用 50% 的可用空间,但如果需要会换行?