简体   繁体   English

使用事件处理程序删除图像

[英]Removing images using event handlers

I really can't figure out how to solve this problem.我真的不知道如何解决这个问题。 Here is the question and the original code.这是问题和原始代码。

Question: Implement the setup function that registers a click event handler and implements the following logic: When the button of class remove is clicked, its parent element should be removed from the gallery.问题:实现注册一个click事件处理函数的setup函数,实现如下逻辑:当类remove的按钮被点击时,其父元素应该从图库中移除。

function setup() {
  **//IM SUPPOSED TO PUT MY CODE ONLY IN THIS PART//**
}

// Example case. 
document.body.innerHTML = `
<div class="image">
  <img src="firstimage.jpg" alt="First">
  <button class="remove">X</button>
</div>
<div class="image">
  <img src="secondimage.jpg" alt="Second">
  <button class="remove">X</button>
</div>`;

setup();

document.getElementsByClassName("remove")[0].click();
console.log(document.body.innerHTML);

This is what I have.这就是我所拥有的。 As soon as I run the program, it removes the first image without the user clicking on it.一旦我运行该程序,它会删除第一个图像,而无需用户单击它。 And I have no idea how to fix it.我不知道如何解决它。

function setup() {
    var myImage = document.getElementsByClassName("image");
    document.getElementsByClassName("remove")[0].
    addEventListener("click", function(){
    myImage[0].parentNode.removeChild(myImage[0]);}); 
}

// Example case. 
document.body.innerHTML = `
<div class="image">
  <img src="firstimage.jpg" alt="First">
  <button class="remove">X</button>
</div>
<div class="image">
  <img src="secondimage.jpg" alt="Second">
  <button class="remove">X</button>
</div>`;

setup();

document.getElementsByClassName("remove")[0].click();
console.log(document.body.innerHTML);

The getElementsBy* methods return HTMLCollections, which can be difficult to work with. getElementsBy* 方法返回 HTMLCollections,这可能很难处理。 Consider using querySelectorAll instead, which returns a static NodeList - unlike an HTMLCollection, it can be iterated over directly, it won't change while it's being iterated over, and it's much more flexible.考虑使用 querySelectorAll 代替,它返回一个静态 NodeList - 与 HTMLCollection 不同,它可以直接迭代,它在迭代时不会改变,而且它更灵活。

You want to iterate over each element, which is a lot more elegant than assigning to each element in the collection individually, so try something like this instead:您想遍历每个元素,这比单独分配给集合中的每个元素要优雅得多,因此请尝试以下操作:

document.querySelectorAll('.remove')
  .forEach(button => 
    button.addEventListener('click', () => button.parentElement.remove())
  )

.remove removes an element from the DOM. .remove从 DOM 中移除一个元素。

The reason why the first image is removed automatically before you even click on it is because of the "document.getElementsByClassName("remove")[0].click();"第一个图像在您单击它之前就被自动删除的原因是因为“document.getElementsByClassName("remove")[0].click();” which is directly under the "setup()" function call.它直接在“setup()”函数调用下。 Which means as soon as the function is called to perform the task, "document.getElementsByClassName("remove")[0].click();"这意味着只要调用该函数来执行任务,“document.getElementsByClassName("remove")[0].click();” is immediately performed and removes the first image using index 0 and click().立即执行并使用索引 0 和 click() 删除第一个图像。 So to solve this, try removing that [0] index or remove "document.getElementsByClassName("remove")[0].click();"因此,要解决此问题,请尝试删除该 [0] 索引或删除“document.getElementsByClassName("remove")[0].click();" which is not useful in your case, and see how it goes.这对您的情况没有用,看看它是如何进行的。

    function setup() {
    let myImage=document.querySelectorAll(".remove").
    forEach(function (button){
    button.addEventListener('click', function(){
        button.parentElement.remove()
    })});
}

// Example case. 
document.body.innerHTML = `
<div class="image">
  <img src="firstimage.jpg" alt="First">
  <button class="remove">X</button>
</div>
<div class="image">
  <img src="secondimage.jpg" alt="Second">
  <button class="remove">X</button>
</div>`;

setup();

document.getElementsByClassName("remove").click();
console.log(document.body.innerHTML);

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

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