我有一个如下多个表格,我想在我的第一张桌子上每隔2秒打印一次名字而不打断table2,如果我使用innerHTML它总是打印在顶部(在table1& table2之上,但是我需要在我的案例table1中的特定位置上的变量,如果我在change()函数上使用document.write它会无限循环函数而不会出现意味着table1& table2没有显示?你能不能请建议更好的解决方法吗?
我在update.php上有以下代码
<html>
<head>
<link href="css/sample.css" rel="stylesheet" type="text/css" />
<div id="changeText" ></div>
<script type="text/javascript">
var text = ["Welcome", "David", "Paul", "John"];
var counter = 0;
var elem = document.getElementById("changeText");
function change() {
elem.innerHTML = text[counter];
counter++;
if(counter >= text.length) { counter = 0;
}
</script>
<body>
<tr>
<td>
<table align="left" width=15%>
<td <B><font color="green">Name:<script type="text/javascript">setInterval(change, 2000);
</script>
</font><br>
</td>
</table>
<table id="sample" width=70% cellpadding="2" cellspacing="1" border="1">
<th class="sno" colspan="2">SNO</th>
<th class="fname" colspan="2">FIRST NAME</th>
<th class="lname" colspan="2">LAST NAME</th>
</table>
</td>
</tr>
</body>
</html>
我的目标是在此标记下打印文本数组
<td <B><font color="green">Name:<script type="text/javascript">setInterval(change, 2000);</script></font>
它应该每隔2秒继续打印一次名称更改......
答案 0 :(得分:0)
您遇到了一些语法错误:
<html>
<head>
<link href="css/sample.css" rel="stylesheet" type="text/css" />
</head>
<body>
<tr>
<td>
<table align="left" width=15%>
<td><font color="green">Name:</font>
<span id="myname"></span>
</td>
</table>
<table id="sample" width=70% cellpadding="2" cellspacing="1" border="1">
<th class="sno" colspan="2">SNO</th>
<th class="fname" colspan="2">FIRST NAME</th>
<th class="lname" colspan="2">LAST NAME</th>
</table>
</td>
</tr>
</body>
</html>
<script>
var text = ["Welcome", "David", "Paul", "John"];
var counter = 0;
var elem = document.getElementById("myname");
function change() {
elem.innerHTML = text[counter];
counter++;
if (counter >= text.length) counter = 0;
}
change();
setInterval(function() {
change()
}, 2000);
</script>