简体   繁体   English

仅删除父 div 中的 div

[英]remove only the div inside the parent div

In this websites the user can add as much boxes as he wants, and every box contains a green and blue small boxes, the user should be able to click the blue box to remove the green box.在这个网站中用户可以添加任意数量的盒子,每个盒子包含一个绿色和蓝色的小盒子,用户应该可以点击蓝色盒子来移除绿色盒子。 the issue is that every time I click the blue box it doesn't remove the green box unless there is only one parent box is made.问题是每次我单击蓝色框时它都不会删除绿色框,除非只有一个父框。 I have tried a lot of ways but nothing is working.我尝试了很多方法,但没有任何效果。

let count = 0;
function addBox() {
    let box = `
    <div class="box">
        <div class="lbox" id="lbox">

        </div>
        <div class="rbox" id="rbox">

        </div>
        <h1>
            ${count}
        </h1>
    </div>
    `
    $(`#boxes`).append(box);

    document.getElementById("lbox").addEventListener("click", function() {
        rbox.remove();
    })

    count++;
}

I think document.getElementById will always select the first element only with the given id.我认为document.getElementById将始终 select 仅具有给定 id 的第一个元素。 Therefore only the first lbox element in the dom keeps getting more and more eventlisteners attached to it, while the others won't get any.因此,只有 dom 中的第一个 lbox 元素不断获得越来越多的事件监听器,而其他的则不会。 Make the id's of your elements unique by appending the count.通过附加计数使元素的 id 唯一。 That will make sure that every element gets it's eventlistener:这将确保每个元素都获得它的事件监听器:

let count = 0;
function addBox() {
    let box = `
    <div class="box">
        <div class="lbox" id="lbox${count}">

        </div>
        <div class="rbox" id="rbox${count}">

        </div>
        <h1>
            ${count}
        </h1>
    </div>
    `;
    $(`#boxes`).append(box);

    document.getElementById("lbox" + count).addEventListener("click", function() {
        $(".rbox" + count).remove();
    })

    count++;
}

id must, at all times, be unique per-document . id必须始终是每个文档唯一的 Learn about this very basic here: https://www.w3schools.com/hTML/html_id.asp .在这里了解这个非常基本的信息: https://www.w3schools.com/hTML/html_id.asp Your code keeps readding the same id values over and over, making your HTML invalid and your code dysfunctional.您的代码一遍又一遍地读取相同的 id 值,使您的 HTML 无效并且您的代码功能失调。

Here's a working code example that doesn't rely on ids to get the job done:这是一个不依赖 id 来完成工作的工作代码示例:

 let count = 0; function addBox() { let box = document.createElement('div'); box.className = 'box'; box.innerHTML = ` <div class="lbox"> </div> <div class="rbox"> </div> <h1> ${count} </h1>`; document.getElementById('boxes').appendChild(box); box.querySelector('.lbox').addEventListener('click', function() { box.querySelector('.rbox').remove(); }) count++; }
 .lbox, .rbox { display: inline-block; height: 40px; width: 40px; }.lbox { background-color: blue; }.rbox { background-color: green; }
 <button onclick="addBox()">Add Box</button> <div id="boxes"></div>

If you have more than one parent box you need to iterate over each one.如果您有多个父框,则需要遍历每个父框。 You need to do something like;你需要做类似的事情;

let boxes = document.querySelectorAll('.box');
boxes.forEach(function(box){
box.querySelector('lbox').addEventListener('click',function(){
box.remove();
});
})

I haven't tested this, but the key part is the forEach function.这个我没测试过,但是关键部分是forEach function。 This means everything you do inside the function is scoped to that box.这意味着您在 function 内所做的一切都在该框内。

you need to define to delete the other box inside the same parent div.您需要定义删除同一父 div 内的另一个框。 I would delete the id because the defenition in class is the same.我会删除 id,因为 class 中的定义是相同的。 I would also change the class names to something, wich makes visible what the green and what the blue box is.我还将 class 名称更改为某个名称,这样可以看到绿色和蓝色框是什么。 You can do following:您可以执行以下操作:

let count = 0;
function addBox() {
    let box = `
    <div class="box_wrapper">
        <div class="blue_box">

        </div>
        <div class="green_box">

        </div>
        <h1>
            ${count}
        </h1>
    </div>
    `
    $(`#boxes`).append(box);

    $( ".blue_box" ).click(function() {
         $(this).parent().find(".green_box").remove();
    });

    count++;
}

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

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