简体   繁体   中英

remove image without ID from div with ID

I have the following html:

<div id="container"><img src="some/path/image.jpg" /></div>

I want to remove that image by using a function. However, i'm not very experienced with javascript and I can't get it done because the image element has no ID or class.

  $("button").click(function () {
      $("container").remove("container > img");
    });

However obviously that doesn't work. Please help!

$("#container").find("img").remove(); 

这应该够了吧。

Your code is perfect. However instead of just using the container, You should use # before it.

$("#container > img")

$("container") should be $("#container").

I would suggest $("#container").empty()

try this, could works...

 $("button").click(function () {

//get the images

var myImages = $('#container').find('img');

for (var i =0; i< myImages.length; i++){
//remove each image
$('#container').removeChild(myImages[i]);
}



        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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