How to use jQuery to get elements with the same z-index?

前端 未结 2 788
鱼传尺愫
鱼传尺愫 2021-01-15 21:54

Right now, I have elements with different z-index, and I want to group them using jQuery.

相关标签:
2条回答
  • 2021-01-15 22:22

    If you add this function, then you will have them

    $.fn.filterByZIndex = function(zIndex) {
        var $zElm = $("*").filter(function() {
            return $(this).css('z-index') == zIndex;
        });
        return $zElm;
    };
    
    0 讨论(0)
  • 2021-01-15 22:25

    Create your own custom selector:

    $.extend($.expr[':'], {
      zindex: function(el, i, m) {
        return $(el).css("z-index") === m[3];
      }
    });
    

    Which you could then use like this:

    $("div:zindex(2)").each(function(){
     alert( this.innerHTML );
    });
    

    This will return all div elements with a z-index of 2.

    <div style="position:relative; z-index:2">Foo</div>
    <div style="position:relative; z-index:6">Bar</div>
    

    With the above markup, the earlier jQuery script will alert Foo.

    Demo: http://jsbin.com/icevih/2/edit

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