我知道这可能是一件简单的事情,但我无法弄明白.我试图将一些来自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>
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
art*_*ung 82
<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>
Run Code Online (Sandbox Code Playgroud)
Jon*_*and 32
附加文本节点如下
var td1 = document.getElementById('td1');
var text = document.createTextNode("some text");
td1.appendChild(text);
Run Code Online (Sandbox Code Playgroud)
DVK*_*DVK 14
有几种选择......假设您找到了TD,var td = document.getElementyById('myTD_ID');您可以这样做:
td.innerHTML = "mytext";
td.textContent= "mytext";
td.innerText= "mytext"; - 这个可能不适用于IE之外?不确定
使用firstChild或children数组作为前面提到的海报.
如果它只是需要更改的文本,textContent更快,更不容易发生XSS攻击(https://developer.mozilla.org/en-US/docs/Web/API/Node.textContent)