javascript 不设置 td 标签的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15023356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript doesn't set the value of td tag
提问by sriram
I'm very new to JavaScript and I wish I could set the value of a td
tag using JavaScript.
我对 JavaScript 很陌生,我希望我可以td
使用 JavaScript设置标签的值。
I have code like this to do so:
我有这样的代码:
window.onload = function() {
document.getElementById("units").value = "122"
}
And I have a html file like this:
我有一个这样的 html 文件:
<table class="table" width="100%">
<caption class="bold">TNEB UnitCalculator</caption>
<tbody>
<tr>
<td>testing</td>
<td id="units"></td>
</tr>
</tbody>
</table>
But that doesn't seems to be working!
但这似乎不起作用!
回答by PeeHaa
The td
tag doesn't have a value attribute:
该td
标签没有 value 属性:
document.getElementById("units").appendChild(document.createTextNode(122));
Or if you want to set some attribute:
或者如果你想设置一些属性:
document.getElementById("units").setAttribute('data-value', 122);
回答by sundar nataraj
Actually your code is working correctly
实际上你的代码工作正常
<script>
window.onload = function() {
document.getElementById("units").value = "122"
}
</script>
<table class="table" width="100%">
<caption class="bold">TNEB UnitCalculator</caption>
<tbody>
<tr>
<td>testing</td>
<td id="units"></td>
</tr>
</tbody>
</table>
You can check this in your browsers developer tools. In the command line, type:
您可以在浏览器的开发人员工具中检查这一点。在命令行中,键入:
document.getElementById("units").value
回答by marekful
The td
element does not have a value
attribute. Use innerHTML
instead.
该td
元素没有value
属性。使用innerHTML
来代替。