javascript根据发送到函数的索引突出显示单词
我想突出显示函数传递的特定单词索引,该函数最初是通过单击按钮添加的。 问题是当我重新分配'document.getElementById('inputText').innerHTML=filterText强>' 它重新呈现页面(因为有iframe视频),并且有一个iframe和另一个相互冲突的东西javascript根据发送到函数的索引突出显示单词,javascript,jquery,Javascript,Jquery,我想突出显示函数传递的特定单词索引,该函数最初是通过单击按钮添加的。 问题是当我重新分配'document.getElementById('inputText').innerHTML=filterText' 它重新呈现页面(因为有iframe视频),并且有一个iframe和另一个相互冲突的东西 function makeALlIndex() { defaultText = document.getElementById('inputText').innerHTML; } functio
function makeALlIndex() {
defaultText = document.getElementById('inputText').innerHTML;
}
function changeColor(index, word) {
myText = defaultText;
var filterText = myText.substr(0, index) + '<span class="highlightClass">' + word + '</span>' + myText.substr(index + word.length);
document.getElementById('inputText').innerHTML = filterText;
}
函数makeALlIndex(){
defaultText=document.getElementById('inputText').innerHTML;
}
函数changeColor(索引、字){
myText=defaultText;
var filterText=myText.substr(0,索引)+''+word++''+myText.substr(索引+word.length);
document.getElementById('inputText')。innerHTML=filterText;
}
Html
<body onload="makeALlIndex()">
<div id="inputText">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.
<button onclick="changeColor(0,'')">click me</button>
</div>
</body>
洛雷姆·伊普苏姆·多洛尔·希特(Lorem ipsum Door sit amet)是一位杰出人士,他是一位临时总统。
点击我
尝试将方法更改为
function changeColor(index, word) {
myText = defaultText;
var filterText = myText.substring(0, index).split( word ).join( '<span class="highlightClass">' + word + '</span>' ) + myText.substr(index + word.length);
document.getElementById('inputText').innerHTML = filterText;
}
函数changeColor(索引、字){
myText=defaultText;
var filterText=myText.substring(0,index).split(word).join(“”+word+“”)+myText.substr(index+word.length);
document.getElementById('inputText')。innerHTML=filterText;
}
@Aliraza-
我认为下面的解决方案对你有用
function changeColor(index, word) {
myText = defaultText;
var filterText = '',newText ='',myTextArray =[];
if( myText.indexOf(word)!=-1){
myTextArray = myText.split(word);
for(i=0; i<= myTextArray.length-1;i++){
if(i==index){
newText = myTextArray[i] + '<span class="highlightClass">' + word + '</span>';
}else {
newText = myTextArray[i];
}
filterText += newText;
}
document.getElementById('inputText').innerHTML = filterText;
}
}
函数changeColor(索引、字){
myText=defaultText;
变量filterText='',newText='',myTextArray=[];
if(myText.indexOf(word)!=-1){
myTextArray=myText.split(word);
对于(i=0;iso)您想突出显示innerHTML
中特定文本的颜色吗?是的,我只获取索引每次更改都必须删除上一个,因为它的阅读效果这已经被询问了很多次…当我注释“document.getElementById('inputText')时,它可能会重复加载我的页面.innerHTML=filterText;'很好,但如何替换已过滤的html@AliRaza未了解如何替换过滤后的html部分。当我在filterText
中有新的html时,因此当我将其分配给内部html时,它将重新显示我的页面。@AliRaza如果再次调用changeColor,预期的行为是什么?删除上一个IOU突出显示的单词并应用于下一个单词(由索引表示)