2017-02-10 5 views
0

私はこのコードをジャバスクリプトに持っています。これは、ボタンを作成し、onclickの機能を持っていますが、.VALUEはbutton.valueはボタンのテキスト値を変更していません

var button = document.createElement("button"); 
button.value = "+"; 
button.onclick = addBox; 
document.getElementById("section").appendChild(button); 
+0

は、あなたが "' .value'は動作しません" によって何を意味するか追加することができます?あなたは何を期待していますか? – evolutionxbox

+0

'addBox'関数も表示できますか? –

+0

ようこそStackOverflow!あなたの質問に精通してください。何かが「うまくいきません」と言っているのは、他のユーザーが正しい答えを提供するのに十分なほどの記述ではありません。あなたは何を達成しようとしていますか?何を試しましたか? –

答えて

1

ないvalueしかしtextContent動作しません。値を使用する場合は、<input type="button" />を使用する必要があります。 ButtonのテキストはtextContentです。

なぜtextContentであり、innerHTMLでないと、textContentが高速であるため、htmlに解析しようとしません。タイプと入力の場合

var button = document.createElement("button"); 
 
button.textContent = "+"; 
 
document.getElementById("section").appendChild(button);
<div id="section"> 
 
    
 
</div>

=ボタン

var button = document.createElement("input"); 
 
button.type = "button" 
 
button.value = "+"; 
 
document.getElementById("section").appendChild(button);
<div id="section"> 
 
    
 
</div>

+0

.i-i-n-n-e-r-T-e-x-t- '.textContent'を使用することもできます。 '.value'はボタン上で有効ですが、テキストとして表示されません。 – evolutionxbox

+0

もっと "正しく": 'button.appendChild(document.createTextNode(" + "));' –

+1

@evolutionxbox 'innerText'は古いIEだけです。あなたは 'textContent'を望みます。 –

関連する問題