Jquery 选择父级中具有图像的所有元素

Jquery 选择父级中具有图像的所有元素,jquery,image,plugins,jquery-selectors,Jquery,Image,Plugins,Jquery Selectors,所以我正在编写一个jQuery插件来应用视网膜图形。我需要选择一个元素(如果它有背景图像或源图像),然后选择其中的每个元素(如果它有背景图像或源图像) 这是我的插件: //RetinizeJS (function($){ $.fn.retinize = function(){ $(this).filter(function(){ if (this.currentStyle) return this.currentStyle['backgr

所以我正在编写一个jQuery插件来应用视网膜图形。我需要选择一个元素(如果它有背景图像或源图像),然后选择其中的每个元素(如果它有背景图像或源图像)

这是我的插件:

//RetinizeJS
(function($){
    $.fn.retinize = function(){
    $(this).filter(function(){
        if (this.currentStyle) 
            return this.currentStyle['backgroundImage'] !== 'none';
        else if (window.getComputedStyle)
            return document.defaultView.getComputedStyle(this,null)
                .getPropertyValue('background-image') !== 'none';
    }).addClass('bg_found');
    };
})(jQuery);

$('body').retinize();
如果body元素具有背景图像,则需要返回body元素;如果body元素是
或如果它们具有CSS
背景图像
属性,则需要返回body元素

我怎样才能做到这一点

晚上9:13更新:

这不会将类HAS|IMAGE添加到任何内容中,如果我删除
|| |($This.css('backgroundImage')!='none'),它仍然不会选择
元素


您可以使用
.CSS()
检查元素是否具有特定的CSS规则值。我认为(即未测试)通过检查
.css('backgroundImage')
,您应该能够检查元素是否具有背景图像,无论它是否使用css
background
或css
background image
设置

您可以使用这些知识设置一个过滤函数,如下所示:

$(selector).filter(function () {
    var _this = $(this);
    return _this.is('img') || (_this.css('backgroundImage') !== 'none');
});
你只需要研究它是如何通过你的插件级联到后代中的

但是,如果您必须严格地从
进行检查,那么扩展
$.fn
听起来是错误的选择
$.fn
扩展用于作用于jQuery对象的函数,如
$('body').retinize()
$('div img').retinize()


你可能想通过直接扩展到
$
,把它变成一个实用函数,这样你就可以通过
$.retinize()
或其他什么来调用它。

我的阴谋论者开始怀疑在过去几天里有一个向下投票的机器人。我不明白为什么这会被否决。谁否决了?没有反对票,这不是问题所在。我一直有过滤图像的代码。我需要选择一个元素及其子元素。像这样:
$(这个,这个*)
,但不幸的是,这不起作用。哦,你试过
$(这个)。找到('*')。和self()
?执行
.find('*')
应该会得到所有子代(对于子代,您只需要
.children()
),然后调用
.andSelf()
将父容器添加回集合中(即children+parent)。另外,使用答案中的代码,这也会选择具有速记背景图像的元素吗?还没有尝试,但应该会™. 根据经验,确实如此,但这可能取决于浏览器的细微差别。刚刚测试过,至少在最近的每个浏览器中都是如此。
$(selector).filter(function () {
    var _this = $(this);
    return _this.is('img') || (_this.css('backgroundImage') !== 'none');
});