find elements with position:attribute

前端 未结 3 1072
无人共我
无人共我 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');

    0 讨论(0)
  • 2020-12-31 09:21

    You could use filter()

    $('*').filter(function(){
       var position = $(this).css('position');
       return position === 'absolute';
    });
    

    You can't use attribute equals selector because because that selector would search elements with an attribute called position that equals absolute like this

     <div position="absolute">
    

    but in your case position is a css property

    0 讨论(0)
  • 2020-12-31 09:39

    Try this:

    $("*[style*='position:absolute']").each (function () {
         alert($(this).html());
    });
    

    Demo : http://jsfiddle.net/XRRbr/1/

    More info: http://api.jquery.com/attribute-contains-selector/

    0 讨论(0)
提交回复
热议问题