小编Gao*_*Fan的帖子

使用 JS 在文本中查找并突出显示单词

我是 Javascript 新手。我有一个问题:如何突出显示文本中输入的单词。我只能做检查这个词是否在文本中的部分,但无法在文本中突出显示这个词。提前谢谢大家!

   var paragraph = document.getElementById('paragraph').innerText; //PARAGRAPH
        input = document.getElementById('typed-text').value; //TYPED TEXT
        textIncludes = paragraph.includes(input); // TEXT INCLUDES WORD
    
    
    if (textIncludes === true) {
        alert('Word has been found')
    } else {
        alert('No matches found')
    }
Run Code Online (Sandbox Code Playgroud)
 <div id="highlights">
            <div class="container">
                <div class="row">
                    <div class="col-md-12" id="paragraph">
                        <p>
                            Robotics is an interdisciplinary branch of engineering and science that includes mechanical engineering, electronics engineering, information engineering, computer science, and others. Robotics deals with the design, construction, operation, and use of robots, as well …
Run Code Online (Sandbox Code Playgroud)

html javascript input highlight include

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

highlight ×1

html ×1

include ×1

input ×1

javascript ×1