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

前端 未结 5 1721
情歌与酒
情歌与酒 2020-12-08 12:42

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.



        
5条回答
  •  粉色の甜心
    2020-12-08 13:36

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

    • 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.

    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)

提交回复
热议问题