javascript 如何用Javascript写入表格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15893345/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:29:29  来源:igfitidea点击:

How to write into a table with Javascript?

javascripthtmlhtml-tablegetelementbyid

提问by shermanzach

<Script>
function getExperience()
{
    var xp = document.getElementById('txt_XP').value;
    document.getElementByID("plank").innerHTML = xp/30;
}
</Script>

So here is my code, and my problem is that I seem to be unable to write over data in a table with the id's planl, oakPlank, teakPlank, and mahoganyPlank. I am thinking that I may be making an obvious mistake to someone who has done this sort of thing before, but I can't seem to catch it. Any help is much appreciated, and here is a snippet of my table, if it helps:

所以这是我的代码,我的问题是我似乎无法用 id 的 planl、oakPlank、teakPlank 和 mahoganyPlank 覆盖表中的数据。我想我可能对以前做过这种事情的人犯了一个明显的错误,但我似乎无法抓住它。非常感谢任何帮助,如果有帮助,这里是我的表格片段:

<tr>
    <td>Plank</td>
    <td id="plankXP">30</td>
    <td id="plank">0</td>
</tr>

EDIT: I didn't realize that this may be pertinent, my bad. This is the form I used to get input, which after putting an alert in to see if it could retrieve the XP, it functioned correctly:

编辑:我没有意识到这可能是相关的,我的错。这是我用来获取输入的表单,在放入警报以查看是否可以检索 XP 后,它可以正常运行:

<form name="experience" id="experience_frm" action="#">
    Experience: <input type="text" name="XP" id="txt_XP"/>
    <input type="submit" value="Go" onclick="getExperience();"/>
</form>

回答by Ian

You have used the wrong documentmethod. Javascript is case sensitive. You used:

您使用了错误的document方法。Javascript 区分大小写。你用过:

document.getElementByID

for getting the id="plank"element. But you need to use:

用于获取id="plank"元素。但是你需要使用:

document.getElementById

Notice the d(last character) change.

注意d(最后一个字符)的变化。

With this change, a simple example works for me:

有了这个改变,一个简单的例子对我有用:

http://jsfiddle.net/wqZAq/

http://jsfiddle.net/wqZAq/

回答by Ravi

Try This

试试这个

<Script>
function getExperience()
{
    var xp = document.getElementById('txt_XP').value;
    var newxp = parseInt(xp);
    var div = newxp/30;
    document.getElementById("plank").innerHTML = div;
}
</Script>
<table>
<tr>
    <td>Plank</td>
    <td id="plankXP">30</td>
    <td id="plank">0</td>
</tr>
</table>
<input type="text" id="txt_XP" name="txt_XP" />
<input type="button" onclick="getExperience();" />