0

我有多个表格,如下所示,我想在第一个表格上每 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 秒打印一次并更改名称...

4

1 回答 1

0

你有一些语法错误:

http://jsfiddle.net/gFWeF/6/

<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>
于 2013-02-06T08:39:25.533 回答