通过单击 Javascript 中的按钮在表格单元格中设置文本

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

Set text in table cell by clicking a button in Javascript

javascripthtmldom-eventsoracle-apex

提问by Csabi

I have a problem I wrote a code I have a table and I want to set text to one of my cell in table on button click:

我有一个问题我写了一个代码我有一个表格,我想在单击按钮时将文本设置为表格中的一个单元格:

<html>
<head>
<script type="text/javascript">
function navratna()
{
var y=document.getElementById("navrat");
y.value="ahoj";
}
</script>
</head>
<body>
<table border="1">
<tr>
<td height="20" width="100" id="navrat">
</td>
</tr>
</table>
<input type="button" value="pokus" onclick="navratna()"/>
</body>
</html>

Please can you help me?

请你能帮帮我吗?

采纳答案by Hmerman6006

You could also use innerText:

您还可以使用innerText

function navratna()
{
var y=document.getElementById("navrat");
y.innerText="ahoj";
}
<body>
    <table border="1">
<tr>
<td height="20" width="100" id="navrat">
</td>
</tr>
</table>
<input

 type="button" value="pokus" onclick="navratna()"/>
</body>

回答by Felix Kling

valueis a property of form elements only. You have to use innerHTML:

value仅是表单元素的属性。你必须使用innerHTML

function navratna()
{
    var y = document.getElementById("navrat");
    y.innerHTML = "ahoj";
}

There are also various other attributes to set only text (which would be the most appropriate in your situation), but they differ from browser to browser. innerHTMLis the best cross-browser way.

还有各种其他属性可以仅设置文本(最适合您的情况),但它们因浏览器而异。innerHTML是最好的跨浏览器方式。

回答by goto-bus-stop

Use innerHTMLinstead of value.

使用innerHTML代替value

function navratna()
{
  var y = document.getElementById("navrat");
  y.innerHTML = "ahoj";
}

回答by ndbroadbent

function navratna()
{
    var y=document.getElementById("navrat");
    y.innerHTML="ahoj";
}

回答by Bhanu Prakash Pandey

use innerHTML

innerHTML

y.innerHTML ="ahoj";