Is there a style selector in jQuery?

前端 未结 5 1241
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 10:27

If I want to select every image which it\'s alt is Home for example, I can do something like this:

$(\"img[alt=\'Home\']\")

But how can I s

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 10:47

    var $images = $("img").filter(function() {
        return $(this).css('width') == '750px';
    });
    

    EDIT: There is no plugin I am aware of, or any plans to include such specific functionality. You can easily pluginify it yourself, such as (untested):

    $.fn.filterByWidth = function(width) {
        var $images = $("img").filter(function() {
            return $(this).css('width') == width;
        });
        return $images;
    };
    

    Usage:

    $images = $('#div img').filterByWidth('750px');
    $images = $('#div img').filterByWidth('50%');
    ...etc...
    

提交回复
热议问题