I'm curious if anyone knows why this piece 开发者_JS百科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,
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.com/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()});
精彩评论