[英]How do I set color attribute each text line on HTML corresponding to javascript code line on execution?
所以说吧,我有一个这样的HTML页面:
<!DOCTYPE html>
<html>
<head>
<script src="script.js"/> <!--this calls my script-->
</head>
<body>
<div>
<ul>
<li>var a = 1;</li>
<li>var a = 2;</li>
<li>var a = 3;</li>
<li>var a = 4;</li>
<li>var a = 5;</li>
</ul>
</div>
<input type="submit" onclick="HighlightCode()" />
</body>
</html>
我的script.js包含:
function HighlightCode()
{
for(var i = 0; i < 5; i++)
{
var a = i + 1;
var ul = document.getElementByTagName("ul");
//get each <li> and set color attribute to red
ul.children[i].style.color = "red";
// reset previous line back to black
if(i != 0)
ul.children[i - 1].style.color = "black";
// some kind of function that delay the loop 1000ms before increasing variable "i"
Sleep(1000);
//I have no idea what the function name is in Javascript, in C# I often use Thread.Sleep(1000);
}
}
我想在每次循环执行时突出显示每个“ li”行!
希望有人能帮忙! 谢谢!
使用CSS和JavaScript:
CSS:
@keyframes test{
0%{color:black}
50%{color:red}
100%{color:black}
}
.blinkyNode{
animation-name: test;
animation-duration: 2s;
animation-timing-function: steps(1,start);
}
javascript:
HighlightCode = function(){
var ul = document.getElementsByTagName("ul")[0];
for(var i = 0; i < ul.children.length;i++){
ul.children[i].className = "";
ul.children[i].style.animationDelay = "";
ul.children[i].offsetWidth = ul.children[i].offsetWidth;
ul.children[i].style.animationDelay = i+"s";
ul.children[i].className = "blinkyNode";
}
}
笔记:
getElementByTagName
不存在。 它是getElementsByTagName
并返回一个数组。
提交按钮仅适用于表单
offsetWidth
= offsetWidth
东西是必需的,因为否则浏览器不会意识到已经读取了该类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.