javascript - 如何在 CSS 中隐藏列表后面的内容?
问题描述
我的目标是将一些内容隐藏在将通过 jQuery 打开/关闭的列表后面。目前,当列表显示时,它会将所有内容下推。但是,我希望它显示为就像它上面的一层一样。我阅读了一些文章并考虑使用z-index
. 虽然一旦我尝试使用它,它就无法正常工作。
$(document).ready(function() {
$("#myList").toggle();
$("#myInput").on("focus", function() {
$("#myList").toggle();
});
$("#myInput").on("focusout", function() {
// Here, it'll wait 100 miliseconds to hide the list.
setTimeout(function() {
$("#myList").toggle();
}, 100);
});
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myList li").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
// This is the code to populate the field after selecting an option.
$("#myList li").on("click", function() {
var texto = $(this).text();
$("#myInput").val(texto);
});
});
.filter-search {
width: 40%;
}
.list-group {
z-index: 1;
}
.box{
width:100px;
height:100px;
}
.red{
background:#f00;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="container">
<h2>Filterable List</h2>
<p>Type something in the input field to search the list for specific items:</p>
<div class="filter-search">
<input class="form-control" id="myInput" type="text" placeholder="Search..">
<br>
<div class="list-group" id="myList">
<a href="#" class="list-group-item list-group-item-action">Cras justo odio</a>
<a href="#" class="list-group-item list-group-item-action">Dapibus ac facilisis in</a>
<a href="#" class="list-group-item list-group-item-action">Morbi leo risus</a>
<a href="#" class="list-group-item list-group-item-action">Porta ac consectetur ac</a>
<a href="#" class="list-group-item list-group-item-action">Vestibulum at eros</a>
</div>
</div>
<div class="box red"></div>
</div>
解决方案
你可以这样做:
.list-group {
position: absolute;
}
推荐阅读
- java - 有什么方法可以向 JList 添加行吗?
- javascript - 如何修复 JS 中未定义的功能
- android - 有什么办法可以改变Material design CardView的特定角的半径
- mysql - select all but the last record from grouped record in a table
- python - Tensorflow NN 精度/预测不变
- node.js - Wait for foreach to complete NodeJS
- python - 如何根据给定的 N 值获取元素列表
- postgresql - Postgresql pg_dump 和 pg restore 不恢复数据库的全部数据
- node.js - 从 AWS SAM 中的节点应用程序连接到 docker 中的 mongodb
- android - 预期为 BEGIN_ARRAY,但在改造调用后为 BEGIN_OBJECT