且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何用javascript一个接一个地写一个元素?

更新时间:2023-02-01 20:05:09

更新:此问题可能无法正确回答问题,但已将其选为正确答案。所以我在这里添加了正确的答案,因为大多数人都没有滚过它并且原作者已经多年没有访问***了。

Update: This question may not answer the question properly but it was selected as the right answer. So I am adding the correct answer here since most people don't scroll past it and the original author hasn't accessed *** for many years now.

var div = document.getElementById( 'div' );
var newText = document.createElement( 'textarea' ); // create new textarea
div.parentNode.insertBefore( newText, div.nextSibling );



旧无关答案:



您可以使用appendChild方法添加新元素

Old Irrelevant answer:

You can use the appendChild method to add a new element

HTML

<div id='div'>
  <textarea></textarea>
</div>

Javascript

Javascript

var div = document.getElementById('div');

var newText = document.createElement('textarea'); // create new textarea

div.appendChild(newText); // add it to the div

生成的HTML

<div id='div'>
  <textarea></textarea>
  <textarea></textarea>
</div>