简体   繁体   English

如何通过单击按钮在 JavaScript 中突出显示多个单词

[英]How to highlight multiple words in JavaScript with button click

I am trying to make it so one or more specific words is highlighted with a button click.我正在尝试通过单击按钮突出显示一个或多个特定单词。

The word is hard-coded in JavaScript.这个词在 JavaScript 中是硬编码的。 I also want this highlighted feature to not be case sensitive.我还希望这个突出显示的功能不区分大小写。

In the below code, the word "agreement" will highlight, but only one word - not multiple words or multiple case types.在下面的代码中,“协议”一词将突出显示,但只有一个词 - 不是多个词或多个案例类型。

The goal is for one button to highlight one or more words and another button to clear the highlight.目标是一个按钮突出显示一个或多个单词,另一个按钮清除突出显示。 I am able to do this but only for one instance of the word.我能够做到这一点,但仅限于这个词的一个实例。

 $('#clickpurpleagreement').click(function() { highlightpurpleagreement('agreement') }); function highlightpurpleagreement(text) { console.log($('#inputText').text()); //inputText = document.getElementById("inputText") var innerHTML = $('#inputText').text(); var index = innerHTML.indexOf(text); if (index >= 0) { innerHTML = innerHTML.substring(0, index) + "<span class='highlightpurpleagreementword'>" + innerHTML.substring(index, index + text.length) + "</span>" + innerHTML.substring(index + text.length); $('#inputText').html(innerHTML); } } $('#clear').click(function() { clearpurple('agreement') }); function clearpurple(text) { console.log($('#inputText').text()); //inputText = document.getElementById("inputText") var innerHTML = $('#inputText').text(); var index = innerHTML.indexOf(text); if (index >= 0) { innerHTML = innerHTML.substring(0, index) + "<span class='clearpurple'>" + innerHTML.substring(index, index + text.length) + "</span>" + innerHTML.substring(index + text.length); $('#inputText').html(innerHTML); } }
 .highlightpurpleagreementword { background-color: #847bba; } .clearpurple { background-color: #ffffff; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <div> <a class="button" id="clear"><strong>Clear Highlight</strong></a> <a class="button button-purple" id="clickpurpleagreement"><strong>Agreement</strong></a> </div> <div class="credits" id="inputText">The agreement, Agreement, or agreement is in review</div>

<div>
  <button id="clear"><strong>Clear Highlight</strong></button>
  <button id="clickpurpleagreement"><strong>Agreement</strong></button>
</div>

<div class="credits" id="inputText">The agreement, Agreement, or agreement is in review</div>
<script>
var colorChanger = document.getElementById("clickpurpleagreement");
colorChanger.addEventListener("click",function() {
  document.getElementById('inputText').style.color = "#847bba";  
});

var colorChanger1 = document.getElementById("clear");
colorChanger1.addEventListener("click",function() {
  document.getElementById('inputText').style.color = "black";  
});
</script>

Instead of indexOf use a Regular Expression to find all occurrences of a substring:代替indexOf使用正则表达式来查找所有出现的子字符串:

 function wordIndexes(text) { var innerHTML = $('#inputText').text(); var regexp = new RegExp(text, 'igm'); var matches = innerHTML.match(regexp); while(match = regexp.exec(innerHTML)) { console.log(match.index); } } wordIndexes('agreement');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="credits" id="inputText">The agreement, Agreement, or agreement is in review</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM