z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。这篇我们主要来看jQuery怎样获取并展示z-index的值?文中有示例代码供大家参考,需要的朋友可以了解看看,接下来就跟随小编一起学习一下吧。
本教程操作环境:windows10系统、jquery3.2.1版本、Dell G3电脑。
jquery怎样查询z-index
css() 方法设置或返回被选元素的一个或多个样式属性。
返回指定的 CSS 属性的值,语法如下:
css("propertyname");
示例如下:
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("z-index = " + $("img").css("z-index"));
});
});
</script>
<style type="text/css">
img
{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<img src="/i/eg_smile.gif" />
<p>由于图像的 z-index 是 -1,因此它在文本的后面出现。</p>
<button>返回img元素的z-index</button>
</body>
</html>