0

重複の可能性:
z-index が最大の DIV を選択

最高の z-index 値を取得するには?
すべての div の z-index 値を配列に入れ、math.max を使用しますか? または任意の提案?

<div class="a" z-index="1">
<div class="a" z-index="3">
<div class="a" z-index="4">
<div class="a" z-index="5">
<div class="a" z-index="6">
<div class="a" z-index="11">...

<div class="b" z-index="//top+1">

js

var top = // highest z-index value;
4

5 に答える 5

3

you could do:

var zIndx = [];
$("div.a").each(function() {
    zIndx.push( $(this).attr("z-index") );
});
console.log( Math.max.apply( Math, zIndx ) );
于 2013-01-29T04:14:07.273 に答える
2

JavaScript's Array.reduce can do the job. .get can get you the array you need:

var top = $(".a").get().reduce(function (a, b) {
   return Math.max(typeof a === 'object' && $(a).attr('z-index') || a,
      $(b).attr('z-index'));
});

http://jsfiddle.net/8jpea/1/

By the way, be careful about using your own self-defined attributes. Go with data-z-index if you can.

于 2013-01-29T04:14:27.590 に答える
2
var maxValue = Math.max.apply( Math, $('.a').map(function() {
    return +$.attr(this, 'z-index');
}));

これがフィドルです:http://jsfiddle.net/ZFhzu/


最新の JavaScript を使用している場合は、それほど凝ったものは必要ありませんapply。代わりに値を分散できます。

let maxValue = Math.max(...$('.a').get().map(el => +$.attr(el, 'z-index')));

これがフィドルです:http://jsfiddle.net/7y3sxgbk/

于 2013-01-29T04:19:41.383 に答える
1

Here's the function you can use.

var index_highest = 0;   

// more effective to have a class for the div you want to search and 
// pass that to your selector

$(".a").each(function() {

         // always use a radix when using parseInt

         var index_current = parseInt($(this).css("zIndex"), 10);

         if(index_current > index_highest) {
               index_highest = index_current;
          }
});

console.log(index_highest);
于 2013-01-29T04:14:19.547 に答える
1
    var top = 0;
    $('.a').each(function(){
        if(top < parseInt($(this).attr('z-index'))){
            top= parseInt($(this).attr('z-index'));
        }
    });
    $('.b').attr('z-index',top+1);

jsfiddle.net/EC36x

于 2013-01-29T04:18:27.540 に答える