2011-07-29 9 views
0
<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> 

私はそれが動作しないjavascriptのDOMの問題

<div type="text" id="txt"> 

<input type="text" id="txt"> 

を変更したいです。 は、私はこの問題は

document.getElementById('txt').value=c; 

だと思うが、私はそれはまだ動作しません

document.getElementById('txt').innerHTML=c; 

を試してみました。 誰かが私にその理由を教えてもらえますか? 乾杯!

+0

これは変です、私は試してみましたhttp://jsfiddle.net/8fpht/のurのコードとそれは動作します。何がうまくいかない?どのブラウザですか?あなたはコンソールでjavascriptエラーを見ることができますか? –

答えて

2

を変更してみてください。あなたが使用する必要があるのdivで入力を置換する場合:

element.replaceChild(newDiv, oldTextbox); 

は試してみてください。

var div = document.createElement("DIV"); 
div.id = "txt"; 
div.innerText = "Hello world!"; 

var tb = document.getElementById("txt"); 
tb.parentElement.replaceChild(div, tb); 
0

は、テキストボックスの値がテキストボックス内のテキストを埋めるために起こっている設定<div type="text" id="txt"><div id="txt">

+0

コメントでなければなりません。 – Pindatjuh

0

は基本的に、あなたはこのことについて尋ねている:

var d = document.createNode("div") 
d.setAttribute("id", "txt") 
d.setAttribute("type", "text"); 
var input = document.getElementById("txt"); 
input.parentElement.replaceChild(d, input); 

をしかし、私が思いますあなたはより良いです:

function timedCount() 
{ 
    document.getElementById('txt').value=c; 
    if(c == 100) return; 
    c=c+1; 
    t=setTimeout("timedCount()",30); 
} 

function doTimer() 
{ 
    if (!timer_is_on) 
    { 
     // set the input to readOnly, this allows JS to update it without 
     // letting the user modify the value. 
     document.getElementById('txt').readOnly = true 
     timer_is_on=1; 
     timedCount(); 
    } 
}