如何用javascript编写一个元素紧接着另一个元素?
我需要在另一个元素(文本区域)之后添加一个 onclick 元素。
我该怎么做?
I need to append an element onclick just after another (a textarea).
How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接受的答案将在这种特定情况下起作用,但要更一般地在另一个元素之后插入一个元素,可以执行以下操作:
其中 newElement 是要插入的元素,someElement是要插入到其后的元素。
W3C insertBefore 方法 节点接口
The accepted answer will work in this specific case, but to insert an element after another more generally, the following does the job:
Where newElement is the element to be inserted and someElement is the element it is to be inserted after.
W3C insertBefore method of the Node interface
更新:此问题可能无法正确回答问题,但已被选为正确答案。所以我在这里添加正确的答案,因为大多数人都不会跳过它,而且原作者已经很多年没有访问 Stackoverflow 了。
@RobG 的正确答案
旧的不相关答案:
您可以使用appendChild方法添加新元素HTML
Javascript
结果 HTML
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.
Correct answer from @RobG
Old Irrelevant answer:
You can use the appendChild method to add a new elementHTML
Javascript
Resulting HTML
outerHTML
方法也能解决这个问题:outerHTML
method does the trick too: