javascript - 如何使用 javascript 更改 HTML 中不匹配单词的颜色

标签 javascript html css highlighting

我有一个 span 和一个 input 字段;当我在输入字段中输入该文本时,我想更改 span 中文本的颜色。

以下是我的代码:

我想,如果我输入错误的单词,那么该单词将在 span 中变红

var i=0;
var idx=0;
document.body.onkeydown = function(e){

    if(e.keyCode == 32 )
    
{
highlight();
}
}
function highlight() {
  var str= document.getElementById("pera").innerText.split(' ');
  var text= str[i];
  var wrdl = text.length;
  var inputText = document.getElementById("pera");
  var innerHTML = inputText.innerText;
	var pretext = innerHTML.slice(0, idx);
	var postext = innerHTML.slice(idx+text.length);
	
  if ( idx >= 0 && text!="")
    {      
var highlightedText = pretext;	
	  highlightedText += "<span class='highlight'>";
		highlightedText += text;
		highlightedText += "</span>";
		highlightedText += postext;
		 		document.getElementById("pera").innerHTML=highlightedText;
    }
	
i++;
idx += parseInt(text.length+1);
}
.highlight
{
background-color:yellow;

}
<span id="pera">This paragraph is a value of span</span>
</br>
<input type="text" id ="inp" onfocus="highlight();" />

最佳答案

此代码应以绿色突出显示匹配的部分,并以红色突出显示不匹配的部分。

它的工作原理是找到用户输入的文本第一次出现的索引,并添加开始和结束 <span>它周围的标签。

function highlight() {

  const text = "This paragraph is a value of span"; //The actual text to compair the value against

  var value = document.getElementById("in").value; //The input value

  var startingIndex = text.indexOf(value); //The string index where the value begins in the paragraph
  
  if (startingIndex!=-1) { //If the value is within the text
  
    var endingIndex = startingIndex+value.length; //The string index where the value ends is just the length of the value added to the starting index
  
    var highlightedText = text.slice(0,startingIndex); //The text from the beginning to the start of the highlight
    highlightedText += "<span style=\"color:green;\">"; //Add the HTML which will cause the highlight
    highlightedText += text.slice(startingIndex,endingIndex); //Add the text to highlight
    highlightedText += "</span>"; //Add the HTML which will cause the end of the highlight
    highlightedText += text.slice(endingIndex); //Add the remaining text
    
    document.getElementById("para").innerHTML = highlightedText; //Set the HTML of the paragraph to be the new, highlighted string that we made.
    
  }
  
}
<span id="para" style="color:red"><span style="color:green">This paragraph is</span> a value of span</span><br><br>
    
<input type="text" id="in" value="This paragraph is" oninput="highlight()">

关于javascript - 如何使用 javascript 更改 HTML 中不匹配单词的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45184143/

相关文章:

javascript - laravel javascript 比较 2 个盒子

html - 来自网络的 href 到 "add to calendar"不适用于 Android 6.0+ 上的 native 谷歌日历应用程序

java - 如何在 Java Web 应用程序中自动对静态文件(css、js、图像)进行版本控制

javascript - 如果除了可见表单之外单击页面上的其他任何位置,则关闭搜索表单

javascript - 使用 jquery ui 自动完成但样式隐藏

html - 我的媒体 CSS 有什么问题?

html - Bootstrap 导航对齐堆栈 li

javascript - 有什么方法可以将状态变量导出/传递到 ReactJS 中的外部 CSS 文件

html - <video> 标签在右侧添加 3px 的空白

javascript - 查询 : animate image from right corner once scrolled to specific position