2011-04-21 92 views
0

我想通過jquery設置z-Index,但我需要每個DIV除了一個名爲importantdiv的從0到100。唯一的問題是重要的是得到40而不是510的z-索引?我在我的jQuery代碼中做了什麼錯誤?jQuery zIndex div顯示問題

<script src="scripts/jquery-1.5.2.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function() { 
     var zIndexNumber = 100; 
     $('div').each(function() { 
      $(this).css('zIndex', zIndexNumber); 
      zIndexNumber -= 10; 
     }); 
     $('#importantdiv').css('zIndex', 510); 
    }); 
</script> 
+0

它的正常工作與我:http://jsfiddle.net/naveed_ahmad/vYYkX /查看演示 – 2011-04-21 18:27:19

+0

使用開發人員工具顯然檢查元素 – 2011-04-21 18:28:05

回答

1

試試這個:

$(function() { 
    var zIndexNumber = 100; 
    $('div').not($('#importantdiv')).each(function() { 
     $(this).css('z-index', zIndexNumber); 
     zIndexNumber -= 10; 
    }); 
    $('#importantdiv').css('z-index', 510); 
}); 
-1

這可能會發生,因爲「每一個」循環功能在一個單獨的線程粉碎覆蓋importantdiv因爲代碼分成兩個線程還挺運行。你可以在循環中放一個If來檢查importantdiv,如果這樣的話跳過它。

1

,一則是:

$(this).css('z-index', zIndexNumber); 

$(this).css('zIndex', zIndexNumber); 
+0

兩者都可以工作。 – thirtydot 2011-04-21 18:31:37