最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remove image without ID from div with ID - Stack Overflow

programmeradmin4浏览0评论

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!

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!

Share Improve this question asked Feb 2, 2012 at 1:32 Daan TwiceDaan Twice 3371 gold badge3 silver badges15 bronze badges 1
  • docs.jquery./Tutorials – user1106925 Commented Feb 2, 2012 at 1:43
Add a ment  | 

4 Answers 4

Reset to default 7
$("#container").find("img").remove(); 

that should do the trick.

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]);
}



        });
发布评论

评论列表(0)

  1. 暂无评论