首页 > 解决方案 > 在 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>

标签: javascripthtmlvue.js

解决方案


推荐阅读