簡體   English   中英

按鈕后自動插入 div 但在新行中

[英]Auto insert div after button but in a new line

我定義了一個按鈕 object 和一個 div object (演示),並使用它們我寫了類似的東西

Usually the <button class=col>cat</button> is a domestic animal.
<div class=con>It is a small mammal.</div>
But it can also be a wild animal.

使用insertAdjacentHtml()可以自動添加 div 元素

var button = document.getElementsByClassName("col")[0];
button.insertAdjacentHTML("afterend", "<div class=con>It is a small mammal.</div>");

但通過這種方式,div 就在按鈕之后添加

Usually the <button class=col>cat</button><div class=con>It is a small mammal.</div> is a domestic animal.
But it can also be a wild animal.

有沒有辦法(javascript?)在包含按鈕的旁邊的行中添加 div?

用什么代替呢?

button.append("<div class=con>It is a small mammal.</div>");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM