0

Is there a neat way to get the topmost element using jQuery?

I don't mean with respect to DOM hierarchy. i.e. .closest() is not what I need.

I mean, with respect to z-index and obviously it should be currently :visible

I was starting out to write a custom selector, but thought it would be better to see if there are better approaches.

4

2 に答える 2

1

これを試して:

elem = [];
$("*").each(function(i){
    elem[$(this).index()] = $(this).css('z-index');
})

function compareNumbers(a,b) {
    return a - b;
}

topmostIndex = elem.sort(compareNumbers).reverse();
alert(topmostIndex[0])
于 2012-06-18T07:36:39.087 に答える
0

これを試して:

$(':visible').each(function(){
    var z = parseInt($(this).css('z-index'), 10);
    vat maxz = -1;
    if (!best || maxz < z) {
        best = this;
        maxz = z;
    }

});
于 2012-06-18T07:28:50.220 に答える