find elements with position:attribute

前端 未结 3 1086
无人共我
无人共我 2020-12-31 08:35

I would try to find all \"absolute\" elements in my page; with jQuery I though it would be something like

$(\'[position=\"absolute\"]\')

bu

3条回答
  •  不思量自难忘°
    2020-12-31 09:15

    Building on Nicola's answer, you can also extend jQuery's selector engine.

    $.extend($.expr[':'],{
        absolute: function(el) {
            return $(el).css('position') === 'absolute';
        },
        relative: function (el) {
            return $(el).css('position') === 'relative';
        },
        static: function (el) {
            return $(el).css('position') === 'static';
        },
        fixed: function (el) {
            return $(el).css('position') === 'fixed';
        }
    });
    

    Then you can you do things like this.

    $(':absolute');

    $('div.sidebar:relative');

提交回复
热议问题