用普通的 javascript 替换 div
我正在寻找一种替换子元素的方法,在下面的代码中,我尝试用 child-div-2 替换 child-div-1,然后用 child-div- 2 by child-div-3,然后 3 by 1 等,在我单击按钮时循环。 (此代码是一个示例,在我的实际代码中,parent-div 内还有 4 个其他 div,每个 div 中有 1 个 span 元素..)
编辑:一次只有一个“parent-div”,其他显示:没有任何。
HTML 代码示例:
<button onclick="replaceContent()"></button>
<div class="parent-div">
<div id="child-div-1">
<span>hello</span>
</div>
</div>
<div class="parent-div">
<div id="child-div-2">
<span>world</span>
</div>
</div>
<div class="parent-div">
<div id="child-div-3">
<span>test</span>
</div>
</div>
JAVASCRIPT 想法和示例测试:
let parentDiv = document.getElementByClassName('parent-div');
let childOne = document.getElementById('child-div-1');
let childTwo = document.getElementById('child-div-2');
let childThree = document.getElementById('child-div-3');
function replaceContent() {
then I am stuck here;
}
我不知道如何设置: 替换儿童(新儿童,旧儿童);
感谢您的帮助 !
I'm looking for a way to replace child element, in the code below, I'm trying to replace child-div-1 by child-div-2, then child-div-2 by child-div-3, then 3 by 1 etc, looping while I click on a button. (This code is an example, in my actual code inside the parent-div there is 4 others divs with 1 span element in each..)
edit : only one "parent-div" at a time, the other ones are on display:none.
HTML CODE EXAMPLE:
<button onclick="replaceContent()"></button>
<div class="parent-div">
<div id="child-div-1">
<span>hello</span>
</div>
</div>
<div class="parent-div">
<div id="child-div-2">
<span>world</span>
</div>
</div>
<div class="parent-div">
<div id="child-div-3">
<span>test</span>
</div>
</div>
JAVASCRIPT ideas & tests :
let parentDiv = document.getElementByClassName('parent-div');
let childOne = document.getElementById('child-div-1');
let childTwo = document.getElementById('child-div-2');
let childThree = document.getElementById('child-div-3');
function replaceContent() {
then I am stuck here;
}
I don't know how to setup this :
replaceChild(newChild, oldChild);
Thank you for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用弹性订单属性
use the flex order property