简体   繁体   English

如何在div中添加和删除图像(javascript和html)

[英]How to append and remove images in a div (javascript and html)

I'm trying to make a function (in javascript) that removes an image I attached to a div in html, I am having trouble getting the image I want to remove. 我正在尝试制作一个函数(使用javascript),以删除我附加到html中的div的图像,但是我无法获取要删除的图像。 The overall idea is to be able dynamically attach and remove images w/o having to use multiple divs. 总体思路是无需使用多个div就可以动态地附加和删除图像。 Here is the function to append the image 这是附加图像的功能

function appendImage(imageSource, Id){
    var img = document.createElement("IMG");
    img.src = imageSource;
    document.getElementById(Id).appendChild(img);
}

The reason I create img in the function (instead of outside) is so that I can attach multiple new ones to the div, if I don't it just replaces the image Here is what I'm trying to for the removing the image 我在函数中(而不是外部)创建img的原因是,我可以将多个新对象附加到div上,如果不这样做,它只是替换图像这是我为删除图像而尝试的操作

function removeImage(imageSource,Id){
    document.getElementById(Id).removeChild(img);
}

I cannot access img in the removeImage because it isn't in the scope. 我无法在removeImage中访问img,因为它不在范围内。 So is there a way to pass img into removeImage w/o making it global or maybe another completely different way not using append or remove? 因此,有没有一种方法可以将img传递到removeImage中而不使其成为全局对象,或者有另一种完全不使用添加或删除的方法?

I don't know if you need it but all i have in my html file is 我不知道您是否需要它,但是我在HTML文件中所拥有的只是

<div id="image"></div>

Edit - added Codepen example: 编辑-添加了Codepen示例:

click for Codepen example 单击以获取Codepen示例

To use removeChild(), it must be called from the parent of the node you are trying to remove. 要使用removeChild(),必须您要删除的节点的父节点中调用它。 Your function would look something like this: 您的函数如下所示:

function removeImage(id) {
    var elementToBeRemoved = document.getElementById(id);
    elementToBeRemoved.parentNode.removeChild(elementToBeRemoved);
}

At its simplest, you could just require one parameter - the id of the desired element - and from that you can determine its parent node and remove it. 简单地说,您只需要一个参数-所需元素的ID-即可确定其父节点并将其删除。

Edit for alternative: 编辑以供选择:

Another thing you could try is have appendImage return something to reference later - either an id that you pass in, or a random number. 您可以尝试做的另一件事是让appendImage返回一些内容以供以后引用-您传入的ID或随机数。 This can be appended to the new img element, and you can reference that to remove it in the future. 可以将其添加到新的img元素中,您可以引用该元素以在将来将其删除。

function appendImage(imageSource, containerId, imageId) {
    var img = document.createElement("IMG");
    img.src = imageSource;
    img.setAttribute('id', imageId);
    document.getElementById(containerId).appendChild(img);
    return imageId;
}

function removeImage(imageId) {
    var elementToBeRemoved = document.getElementById(imageId);
    elementToBeRemoved.parentNode.removeChild(elementToBeRemoved);
}

var myImage = appendImage('my.jpg', 'image', 'testId123');

and later on... 后来...

removeImage(myImage); or removeImage('testId123'); removeImage('testId123');

do you define the variable img in your removeImage function? 您在removeImage函数中定义变量img吗? It doesn't look like you do. 看起来不像您。 In removeImage make sure you find that img somehow and store it's location in a variable that you can then use to remove it removeImage确保以某种方式找到该img并将其位置存储在变量中,然后可以使用该变量将其删除

You could try counting the images in the div and giving each image an identity. 您可以尝试计算div中的图像并为每个图像指定一个标识。 So that you would know which image to remove when it is neccesary. 这样您就可以知道在需要时要删除哪个图像。

function appendImage(imageSource, Id){
    var imagecount = document.getElementById('image').childNodes.length;
    var img = document.createElement("IMG");
    img.setAttribute("id", imagecount + 1);
    img.src = imageSource;
    document.getElementById(Id).appendChild(img);
}
function removeImage(ImageId){
   var elem = document.getElementById(ImageId);
   elem.parentElement.removeChild(elem);
}

You would need to traverse the DOM to find the children of the div. 您需要遍历DOM才能找到div的子代。

function removeImage(imageSource,Id){
    var div = document.getElementById(Id);
    for( var i=0; i<div.childNodes.length; i++){
      div.removeChild(div.childNodes[i]); //you may need an if to only remove the images
    }
}

To build on what Wongesse said 以Wongesse所说的为基础

Modern: 现代:

function removeImage(imageSource,Id){
  var div = document.getElementById(Id);
  var image = div.querySelectorAll('[src="' + imageSource + '"]');

  div.removeChild(image[0]);
}

jQuery: jQuery的:

function removeImage(imageSource,Id){
  $('#' + Id + ' [src="' + imageSource + '"]').remove();
}

IE 8+: IE 8+:

function removeImage(imageSource,Id){
  var div = document.getElementById(Id);
  var images = div.getElementsByTagName('img');

  for(var i = 0; i < images.length; i++){
    if(images[i].src != imageSource){
      // not the image we are looking for
      continue;
    }

    div.removeChild(images[i]);
    break;
  }
}

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

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