简体   繁体   English

如何在JavaScript中将两个按钮连接到一个div

[英]How to two buttons to one div in javascript

I have created one button on my div element and I would like to create another. 我在div元素上创建了一个按钮,而我想创建另一个按钮。

I have already tried var textForButton2 = document.createTextNode("Try again!"); 我已经尝试过var textForButton2 = document.createTextNode(“再试一次!”); button.appendChild(textForButton2); button.appendChild(textForButton2); under the first buttons code but that simply made a bigger button with both texts inside 在第一个按钮代码下,但是只是在其中包含两个文本的地方做了一个更大的按钮

// Adding a button
var button = document.createElement("Button");
var textForButton = document.createTextNode("Next->");
button.appendChild(textForButton);
button.addEventListener("click", function(){
  alert("Hi!");
});
divElement.appendChild(button);

// Appending the div element to body
document.getElementsByTagName("body")[0].appendChild(divElement);

I would like two buttons in my div(which I won't show the code for concise purposes) instead of just the one. 我希望在div中有两个按钮(为简洁起见,我不会显示代码),而不是一个按钮。

Just add another button element. 只需添加另一个按钮元素。

var secondButton = document.createElement("button");
var textForButton2 = document.createTextNode("Try again!"); 
secondButton.appendChild(textForButton2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM