javascript - 如何使用javascript在文本所在的同一行添加按钮?

标签 javascript html dom button alignment

我想在某些文本的右侧添加一个按钮。两个文本都是使用 JS DOM 添加的。我研究了很多,但无法将其放在那里。它总是转到下一行。我该怎么做?

var text = document.createElement("h4");
var content = document.createTextNode("Insert buttton here:--> ");
text.appendChild(content);

document.body.appendChild(text);


var btn = document.createElement("BUTTON");
btn.textContent = "Place me right of the -->";

document.body.appendChild(btn);
html {
  font-size: 29px;
}
<html>

<head></head>

<body>

</body>

</html>

预先感谢您的时间和考虑。

最佳答案

您可以将按钮添加到 h4 的内部 html 中。

var btn = document.createElement("BUTTON");
btn.textContent = "Place me right of the -->";
var text = document.createElement("h4");
text.innerHTML = "Insert buttton here:-->" + btn.outerHTML;

document.body.appendChild(text);
html {
  font-size: 29px;
}
<html>

<head></head>

<body>

</body>

</html>

关于javascript - 如何使用javascript在文本所在的同一行添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51952902/

相关文章:

javascript - 无法对对象数组进行排序

javascript - 从不是用 HTML 编写的 URL 位置自动下载内容。使用软件导航,按下按钮并下载内容

javascript - 在不同的浏览器中输入时间字段以不同的格式显示

javascript - 获取被单击元素在 HTML 源代码中的插入符号 [开始、结束] 位置

javascript - 将特殊的 HTML 字符插入 XML

javascript - 如何将多个属性绑定(bind)到元素?

javascript - 一页上有两个计数器

javascript - 使用 jquery 获取表中特定行的下拉列表的值

javascript - 如何在多个节点上使用 MutationObserver?

javascript - 当手指稍微移动时,点击事件不会在触摸屏上触发