简体   繁体   English

使用Javascript DOM操作将html元素添加到div

[英]Using Javascript DOM manipulation to add html elements to div

I am trying to replicate the following HTML code by using only Javascript (no jQuery). 我正在尝试仅使用Javascript(而不使用jQuery)复制以下HTML代码。 I want the buttons to appear as a group,but it looks like they are being appended individually. 我希望按钮显示为一个组,但是看起来它们是单独添加的。 I've read up on bootstrap button groups ( http://getbootstrap.com/components/#btn-groups ) and the btn-group classs is being called on the html. 我已经阅读了自举程序按钮组( http://getbootstrap.com/components/#btn-groups ),并且在html上调用了btn-group类。 So therefore my javascript DOM manipulation is incorrect. 因此,我的JavaScript DOM操作不正确。

Can someone help me to understand why my buttons are not appearing correctly? 有人可以帮助我了解为什么我的按钮显示不正确吗? Please note that this is only a snippet of the entire code. 请注意,这只是整个代码的一小段。 the HTML elements are nested in a "row" div and "container" div. HTML元素嵌套在“行” div和“容器” div中。

HTML 的HTML

      <div>
    <div class="btn-group btn-group-lg">
      <button type="button" class="btn btn-default">Left</button>
      <button type="button" class="btn btn-default">Middle</button>
      <button type="button" class="btn btn-default">Right</button>
    </div>
  </div>

Javascript Java脚本

var divTwo = document.createElement('div'); 
row.appendChild(divTwo);
col.appendChild(divTwo);

var btnGroupFour = document.createElement('div');
btnGroupFour.className = 'btn-group btn-group-lg';
divTwo.appendChild(btnGroupFour);


var btnLeft = document.createElement('button');
var textLeft = document.createTextNode('Left');
btnLeft.appendChild(textLeft);
btnLeft.className = 'btn btn-default';

var btnMiddle = document.createElement('button');
var textMiddle = document.createTextNode('Middle');
btnMiddle.appendChild(textMiddle);
btnMiddle.className = 'btn btn-default';

var btnRight = document.createElement('button');
var textRight = document.createTextNode('Right');
btnRight.appendChild(textRight);
btnRight.className = 'btn btn-default';

btnGroupFour.appendChild(btnLeft);
btnGroupFour.appendChild(btnMiddle);
btnGroupFour.appendChild(btnRight);

jsfiddle link: https://jsfiddle.net/bchang89/eh7uhs43/2/ jsfiddle链接: https ://jsfiddle.net/bchang89/eh7uhs43/2/

You can use cloneNode() on the parent element .btn-group and set it to a deep copy. 您可以在父元素.btn-group上使用cloneNode()并将其设置为深层副本。 Deep copy will create a copy of the target node as well as it's descendants. 深度复制将创建目标节点及其后代的副本。 The only limitation is that it will not copy any event listeners added to either the target node or it's descendants. 唯一的限制是,它将不会复制添加到目标节点或其后代的任何事件侦听器。

// Collect all .btn-group into a NodeList (btnGrp)
var btnGrp = document.querySelectorAll('.btn-group'); 

// Determine the last .btn-grp by using the .length property -1 
var lastGrp = btnGrp.length - 1;

// Reference the index in the btnGrp NodeList
var tgt = btnGrp[lastGrp];

// Create a clone of tgt and set the parameter to true for deep copy
var dupe = tgt.cloneNode(true);

// Append the clone to the body or any other element you wish.
document.body.appendChild(dupe);

EDIT 编辑

// Appendinding to `.container` since it looks better and makes more sense.
var box = document.querySelector('.container');
box.appendChild(dupe);

Fiddle 小提琴

Snippet 片段

 var box = document.querySelector('.container'); var btnGrp = document.querySelectorAll('.btn-group'); var lastGrp = btnGrp.length - 1; var tgt = btnGrp[lastGrp]; var dupe = tgt.cloneNode(true); box.appendChild(dupe); 
 .btn-default { color: #007aff; background-color: #fff; border-color: #007aff; } .btn-default:hover, .btn-default:focus, .btn-default:active { color: #fff; background-color: #007aff; border-color: #007aff; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="row"> <div class="col-md-12"> <div> <div class="btn-group"> <button type="button" class="btn btn-default">1</button> <button type="button" class="btn btn-default">2</button> <button type="button" class="btn btn-default">3</button> <button type="button" class="btn btn-default">4</button> </div> <div class="btn-group"> <button type="button" class="btn btn-default">5</button> <button type="button" class="btn btn-default">6</button> <button type="button" class="btn btn-default">7</button> </div> <div class="btn-group"> <button type="button" class="btn btn-default">8</button> </div> </div> <hr> <div> <div class="btn-group btn-group-lg"> <button type="button" class="btn btn-default">Left</button> <button type="button" class="btn btn-default">Middle</button> <button type="button" class="btn btn-default">Right</button> </div> </div> </div> </div> </div> 

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

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