在Webkit浏览器上使用jQuery读取和设置z-index值

Net*_*ool 5 css jquery webkit z-index

jQuery("#X").css("z-index");始终在Webkit浏览器上返回"auto".在Firefox上运行正常.

您似乎也无法使用以下内容设置z-index: jQuery("#X").css("z-index",5);

测试用例如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>jQuery CSS Z-index Fail</title>
    <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $("#X").css("z-index",5);       
        $("#Y").append($("#X").css("z-index"));
      });
    </script>
    <style type="text/css">
      h1 {z-index: 3;}
    </style>
  </head>
  <body>
     <h1 id="X">Some Text</h1>
     <div id="Y"> Z-index is: </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

小智 10

您需要指定使用z-index的位置.

<h1 id="X" style='position:relative;'>Some Text</h1>
Run Code Online (Sandbox Code Playgroud)

立即在Chrome中修复它.