如何使用 JavaScript 在带有 id 的 td 中插入文本

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

How to insert text in a td with id, using JavaScript

javascripthtml

提问by Amra

I know it may be a simple thing, but I can't figure out. I am trying to insert some text coming from a JavaScript function onload event into a td.

我知道这可能是一件简单的事情,但我无法弄清楚。我正在尝试将来自 JavaScript 函数 onload 事件的一些文本插入到 td 中。

<html>
 <head>
  <script type="text/javascript">
   function insertText ()
   {
       //function to insert any text on the td with id "td1"
   }
  </script>
 </head>
 <body onload="javascript:insertText()">
  <table>
   <tr>
    <td id="td1">
    </td>
   </tr>
  </table>
 </body>
</html>

Any help?

有什么帮助吗?

回答by artlung

<html>

<head>
<script type="text/javascript">
function insertText () {
    document.getElementById('td1').innerHTML = "Some text to enter";
}
</script>
</head>

<body onload="insertText();">
    <table>
        <tr>
            <td id="td1"></td>
        </tr>
    </table>
</body>
</html>

回答by Jonathan Fingland

append a text node as follows

附加一个文本节点,如下所示

var td1 = document.getElementById('td1');
var text = document.createTextNode("some text");
td1.appendChild(text);

回答by DVK

There are several options... assuming you found your TD by var td = document.getElementyById('myTD_ID');you can do:

有几种选择......假设你找到了你的TD,var td = document.getElementyById('myTD_ID');你可以这样做:

  • td.innerHTML = "mytext";

  • td.textContent= "mytext";

  • td.innerText= "mytext";- this one may not work outside IE? Not sure

  • Use firstChild or children array as previous poster noted.

  • td.innerHTML = "mytext";

  • td.textContent= "mytext";

  • td.innerText= "mytext";- 这个在 IE 之外可能不起作用?没有把握

  • 使用 firstChild 或 children 数组,如前一张海报所述。

If it's just the text that needs to be changed, textContent is faster and less prone to XSS attacks (https://developer.mozilla.org/en-US/docs/Web/API/Node.textContent)

如果只是需要修改的文本,textContent 更快,更不容易受到 XSS 攻击(https://developer.mozilla.org/en-US/docs/Web/API/Node.textContent

回答by Daniel Vassallo

If your <td>is not empty, one popular trick is to insert a non breaking space &nbsp;in it, such that:

如果你<td>的不是空的,一个流行的技巧是在其中插入一个不间断的空格&nbsp;,这样:

 <td id="td1">&nbsp;</td>

Then you will be able to use:

然后你将能够使用:

 document.getElementById('td1').firstChild.data = 'New Value';

Otherwise, if you do not fancy adding the meaningless &nbspyou can use the solution that Jonathan Fingland described in the other answer.

否则,如果您不喜欢添加无意义的内容&nbsp,则可以使用 Jonathan Fingland 在另一个答案中描述的解决方案。

回答by Gabe

Use jQuery

使用jQuery

Look how easy it would be if you did.

看看如果你这样做了会有多容易。

Example:

例子:

$('#td1').html('hello world');