从文档中删除图像,但不是在这个类中

Remove images from document but not in this class

本文关键字:文档 删除 图像      更新时间:2023-09-26

我想摆脱图像在iframe除了在某些类中发现的图像。我已经插入了一些变量:

var class = '.class';
var index = '5';

$('#iframe').contents().find('* img').not(class + ':eq('+ index +')' + ' img').remove();

据我所知,这应该删除除类中的图像以外的所有内容。不管用,所有的图像都被删除了。非选择器似乎没有效果。我做错了什么?我还能尝试什么?

我会使用过滤器,更容易阅读和控制。

$('#iframe').contents().find('img').parent().filter(function() {
    return !($(this).hasClass('class') && $(this).index() == 5);
}).remove();

作为旁注,class是一个保留关键字,对于变量来说不是一个好名字。