87,904
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
var mytime=null;
function ss() {
window.clearInterval(mytime);//取消定时
}
function g() {
mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
}
function myshow() {
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);
//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];
}
</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="s" value='开始' onclick="g()">
<input type="button" id="a" value='停止' onclick="ss()">
</form>
</body>
</html>
你的方法名和按钮的ID冲突了<html>
<head>
<script type="text/javascript">
var mytime=null;
function s(){
window.clearInterval(mytime);//取消定时
}
function g(){
mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
/////////////////////不需要再var/////////////////////////////////////////////////
}
function myshow(){
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);
//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];
}
</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="s1" value='开始' onclick="g()"><input type="button" id="s2" value='停止' onclick="s()"><!--id不能重复-->
</form>
</body>
</html>
<html>
<head>
<script type="text/javascript">
var mytime=null;
function s(){
clearInterval(mytime);//取消定时
}
function g(){
mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
}
function myshow(){
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);
//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];
}
</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="cs" value='开始' onclick="g()"><input type="button" id="cs" value='停止' onclick="s()">
</form>
</body>
</html>
<html>
<head>
<script type="text/javascript">
var mytime=null;
function s(){
window.clearInterval(mytime);//取消定时
}
function g(){
var mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
}
function myshow(){
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);
//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];
}
</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="s" value='开始' onclick="g()"><input type="button" id="s" value='停止' onclick="s()">
</form>
</body>
</html>