JavaScript DOM 问题
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
if(c == 100) return;
c=c+1;
t=setTimeout("timedCount()",30);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
</script>
</head>
<body onload="doTimer()">
<form>
<input type="button" value="Start count!">
<input type="text" id="txt">
</form>
<p>Click on the button above. The input field will count forever, starting at 0.</p>
</body>
</html>
我想换成
<input type="text" id="txt">
不行
<div type="text" id="txt">
。 我认为问题是
document.getElementById('txt').value=c;
但我尝试了
document.getElementById('txt').innerHTML=c;
它仍然不起作用。 有人能告诉我为什么吗? 干杯!!!
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
if(c == 100) return;
c=c+1;
t=setTimeout("timedCount()",30);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
</script>
</head>
<body onload="doTimer()">
<form>
<input type="button" value="Start count!">
<input type="text" id="txt">
</form>
<p>Click on the button above. The input field will count forever, starting at 0.</p>
</body>
</html>
I want to change
<input type="text" id="txt">
into
<div type="text" id="txt">
It doesn't work.
I think the problem is
document.getElementById('txt').value=c;
But I tried
document.getElementById('txt').innerHTML=c;
It still doesn't work.
Could someone tell me why?
Cheers!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置文本框的值将填充文本框中的文本。如果你想用 div 替换输入,你应该使用:
尝试:
Setting the value of the textbox is going to populate the text in the textbox. If you want to replace an input with a div you should use:
Try:
尝试将
更改为
Try changing
<div type="text" id="txt">
to<div id="txt">
基本上,你问的是这样做:
但我认为你的情况会更好:
Basically, you're asking about doing this:
But I think you're better off: