我不知道返回修改中间字母颜色的单词的正确结构。我使用拆分将段落中的所有单词保存在一个数组中,但是我必须修改每个单词,并用红色的中间字母显示它们。调查,我想我可以使用slice方法来修改字体颜色。这是我的代码:
HTML
<body>
<textarea id="text">JavaScript is the Programming Language for the Web,JavaScript can update and change both HTML and CSS,JavaScript can calculate,manipulate and validate data</textarea>
<div class="show">
<h3 id="txt"></h3>
</div>
<div class="control-box">
<button type="button" name="button" onclick="iniciar()">play</button>
</div>
</body>
</html>
脚本
function iniciar(){
var texto = document.getElementById("text").value;
var palabras = texto.split(/[,]+/);
var index = 0;
console.log(palabras);
function tester(){
document.getElementById("txt").innerHTML=palabras[index];
var timer = setTimeout(function(){
console.log(timer);
tester();
},550);
console.log(palabras[index]);
if (index>palabras.length-2){
clearTimeout(timer);
}
index++;
}
tester();
}