jquery - z-index:20 出现在 z-index:10 下面,即使它的 z 索引更大

标签 jquery css z-index

我正在尝试制作一个灯泡。这是代码 JSfiddle

HTML

<input class="button" type="button">
<div id="black" style="display: none; position: fixed; width: 100%; height: 100%; left: 0; top: 0; background: rgba(51,51,51,0.7); z-index: 10;"></div>

<div id="add" style="z-index: 20; display:none; border: 2px solid;">
    <input type="text" >
</div>

查询

$('.button').click(function() {
    $('#add').show();
    $('#black').show();
})

在我看来,当我点击 .button 时,#add 必须出现在 #black 之上,因为它的 z-index( 20) 大于 #black (10)。但它出现在 #black 下。感谢您的帮助。

最佳答案

这是因为z-index属性only applies to positioned elements .

position 属性的默认值为static。如果您将 position: relative 添加到该元素,它将按预期工作。

Updated Example

关于jquery - z-index:20 出现在 z-index:10 下面,即使它的 z 索引更大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28706608/

相关文章:

javascript - jQuery 在 .find() 之后使用选择器

html - 如果旋转了父容器,如何不旋转文本

html - CSS动画使用rotateY翻转半圈

css - postcss 在需要包的 css 文件时找不到配置

html - 父div需要重叠在子div上

html - 绝对定位的嵌套元素上的 z-index

html - 视频上的 z-index 错误不允许显示按钮和文本

jquery - 为什么我在使用 jquery 更改属性后会丢失菜单项上的 css 悬停功能?

javascript - 平滑滚动 + 函数 goToNext 上的偏移

jquery - DataTables row.add() 函数不会添加数组中的所有行