求高手解答一个javascript问题 20
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Document</title><style...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#a{width: 200px;height: 200px;border-color: red;border:3px solid #ccc;}
</style>
<script type="text/javascript">
var c=document.getElementById("a");
var i=0;
var color=new Array("red","blue","yellow","green");
function to()
{
if(i>color.length-1)
{
i=0;
}
else
{
c.style.borderColor=color[i];
i=i+1;
setTimeout("to()",100);
}
}
</script>
</head>
<body>
<div id="a"></div>
<input type="button" value="点击闪烁" onclick="to()">
</body>
</html>
请问为何点击不会变换颜色 展开
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#a{width: 200px;height: 200px;border-color: red;border:3px solid #ccc;}
</style>
<script type="text/javascript">
var c=document.getElementById("a");
var i=0;
var color=new Array("red","blue","yellow","green");
function to()
{
if(i>color.length-1)
{
i=0;
}
else
{
c.style.borderColor=color[i];
i=i+1;
setTimeout("to()",100);
}
}
</script>
</head>
<body>
<div id="a"></div>
<input type="button" value="点击闪烁" onclick="to()">
</body>
</html>
请问为何点击不会变换颜色 展开
展开全部
试了一下,报错:Uncaught TypeError: Cannot read property 'style' of null
C没有加载到,是null…… 可以这么写:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#a{width: 200px;height: 200px;border-color: red;border:3px solid #ccc;}
</style>
<script type="text/javascript">
window.onload = function(){
c=document.getElementById("a");
}
var i=0;
var color=new Array("red","blue","yellow","green");
function to()
{
if(i>color.length-1)
{
i=0;
}
else
{
c.style.borderColor=color[i];
i=i+1;
setTimeout("to()",1000);
}
}
</script>
</head>
<body>
<div id="a"></div>
<input type="button" value="点击闪烁" onclick="to()">
</body>
</html>
上面的代码只能闪烁四次,如果想一直循环,可以这么弄:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#a{width: 200px;height: 200px;border-color: red;border:3px solid #ccc;}
</style>
<script type="text/javascript">
window.onload = function(){
c=document.getElementById("a");
}
var i=0;
var color=new Array("red","blue","yellow","green");
function to()
{
if(i>color.length-1)
{
i=0;
}
c.style.borderColor=color[i];
i=i+1;
setTimeout("to()",1000);
}
</script>
</head>
<body>
<div id="a"></div>
<input type="button" value="点击闪烁" onclick="to()">
</body>
</html>
展开全部
把var c=document.getElementById("a");
放到 to()函数里面,页面还没有加载完成,c就创建了,他会一直是null放到to()里面就能实现了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#a{width: 200px;height: 200px;border-color: red;border:3px solid #ccc;}
</style>
<script type="text/javascript">
var i=0;
var color=new Array("red","blue","yellow","green");
function to()
{
var c=document.getElementById("a");
if(i>color.length-1)
{
i=0;
}
else
{
c.style.borderColor=color[i];
i=i+1;
setTimeout("to()",100);
}
}
</script>
</head>
<body>
<div id="a"></div>
<input type="button" value="点击闪烁" onclick="to()">
</body>
</html>
放到 to()函数里面,页面还没有加载完成,c就创建了,他会一直是null放到to()里面就能实现了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#a{width: 200px;height: 200px;border-color: red;border:3px solid #ccc;}
</style>
<script type="text/javascript">
var i=0;
var color=new Array("red","blue","yellow","green");
function to()
{
var c=document.getElementById("a");
if(i>color.length-1)
{
i=0;
}
else
{
c.style.borderColor=color[i];
i=i+1;
setTimeout("to()",100);
}
}
</script>
</head>
<body>
<div id="a"></div>
<input type="button" value="点击闪烁" onclick="to()">
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-02-10
展开全部
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>menu</title>
<style type="text/css">
#a {
width: 200px;
height: 200px;
border-color: red;
border: 3px solid #ccc;
}
</style>
<script type="text/javascript">
var i = 0;
var color = new Array ("red", "blue", "yellow", "green");
function to ()
{
// 放进去,在外面的时候,dom元素还没有加载完毕,无法获取,采纳即可。
var c = document.getElementById ("a");
if (i > color.length - 1)
{
i = 0;
}
else
{
c.style.borderColor = color[i];
i = i + 1;
setTimeout ("to()", 100);
}
}
</script>
</head>
<body>
<div id="a"></div>
<input type="button" value="点击闪烁" onclick="to()">
</body>
</html>
追问
加进去对了,可是不能循环跳动啊,变换一种颜色就挺住了
追答
你是要循环啊?
把setTimeout改为
setInterval
就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询