如何用javascript一个接一个地写一个元素? [英] How to write an element just after another with javascript?
本文介绍了如何用javascript一个接一个地写一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在一个接一个地附加一个元素onclick(textarea)。
I need to append an element onclick just after another (a textarea).
我该怎么做?
推荐答案
更新:此问题可能无法正确回答问题,但已将其选为正确答案。所以我在这里添加了正确的答案,因为大多数人都没有滚过它并且原作者已经多年没有访问Stackoverflow了。
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 Stackoverflow 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>
这篇关于如何用javascript一个接一个地写一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文