admin管理员组

文章数量:1302382

I'm curious if anyone knows why this piece of jQuery code doesn't remove the images?

var a = $('#tblMain').clone().remove('img');

The table is being selected. This is trying to take the table on the webpage and export to excel but I do not want the images to export.

Thank you,

I'm curious if anyone knows why this piece of jQuery code doesn't remove the images?

var a = $('#tblMain').clone().remove('img');

The table is being selected. This is trying to take the table on the webpage and export to excel but I do not want the images to export.

Thank you,

Share edited May 23, 2011 at 15:11 Michael asked May 23, 2011 at 14:56 MichaelMichael 1522 silver badges13 bronze badges 2
  • 5 (img) refers to a variable name, don't you perhaps want ('img') as a selector? – pimvdb Commented May 23, 2011 at 14:58
  • Put your ment in as the answer! – Brian Seim at EvoDynamic Inc Commented May 23, 2011 at 15:02
Add a ment  | 

2 Answers 2

Reset to default 5

Do it like this:

$("#tblMain").clone().find("img").remove();

EDIT: Okay, here's the problem:

selector: A selector expression that filters the set of matched elements to be removed.

http://api.jquery./remove/

The img in .remove('img') is to filter the set of items in the jquery object, NOT to find elements within the items themselves. In this case, the jquery object contains only one item, the cloned table. Therefore, .remove('img') removes nothing, since the jquery object does not contain any images (only images within items it contains).

I don't know what's happening behind the scenes, but you're referring to some variable called img whilst you most probably just want to select all img elements. In that case, you ought to use a selector as a string:

var a = $('#tblMain').clone().remove('img');

EDIT: .clone.remove does not seem to work indeed. I used this workaround which actually works:

.find('img').each(function() {$(this).remove()});

本文标签: javascriptJQuery remove imagesStack Overflow