span根据给定的js代码,如何在动态插入之前将两者包装在一个div中?
const left = document.querySelectorAll('.left-col span');
const right = document.querySelectorAll('.right-col span');
const rightCol = document.querySelector('.right-col');
const leftCol = document.querySelector('.left-col');
const leftColP = document.querySelector('.right-col');
left.forEach((e, i) => {
leftColP.appendChild(e);
leftColP.appendChild(right[i]);
});
leftCol.remove();
span {
display: block;
}
<div class="row">
<div class="left-col col">
<span>1 line of the left block</span>
<span>2 line of the left block</span>
<span>3 line of the left block</span>
<span>4 line of the left block</span>
</div>
<div class="right-col col">
<span>1 line of the right block</span>
<span>2 line of the right block</span>
<span>3 line of the right block</span>
<span>4 line of the right block</span>
</div>
</div>
1 个回答