gpt4 book ai didi

javascript - 在 bootstrap html 中显示/隐藏一个 div 时自动调整 div 的宽度

转载 作者:太空宇宙 更新时间:2023-11-04 14:30:43 25 4
gpt4 key购买 nike

我正在尝试制作一个包含三个 div 的响应式页面(每个 div 是一列),其中有时会隐藏一列。我的目标是实现:

1.显示三个div时,宽度应为100%。

  1. 当一个div被隐藏时,其他div需要自动调整宽度并适应外部div(即2个div总宽度100%)。

我试过这样做,但宽度是固定的,因为我给了 col-lg 如此。它不是自动调整的:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>

<body>
<div class="row col-lg-12">
<div class="col-lg-4">
<h3>Column 1</h3>
</div>
<div class="col-lg-4">
<h3>Column 2</h3>
</div>
<div class="col-lg-4" hidden>
<h3>Column 3</h3>
</div>
</div>
</div>

</body>
</html>

然后我试着给样式如下:

#whole
{
width:100%;
}

.myfix
{
display:table;
width: 100%;
}

.myfix > .myfix1
{
display:table-cell;
border: 1px solid black;
}
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>


<div class="container">
<div class="row col-lg-12 myfix" id="whole">
<div class="myfix1"> <h3>Column 1</h3> </div>
<div class="myfix1"> <h3>Column 2</h3> </div>
<div class="myfix1" hidden> <h3>Column 3</h3> </div>
</div>

</body>
</html>

但是隐藏是行不通的...我的代码有什么错误吗?请帮忙...也欢迎使用 jquery 的解决方案。提前致谢...

最佳答案

你只需要使用 style="display:none;" 而不是 hidden

下面是工作演示。

#whole
{
width:100%;
}

.myfix
{
display:table;
width: 100%;
}

.myfix > .myfix1
{
display:table-cell;
border: 1px solid black;
}
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>


<div class="container">
<div class="row col-lg-12 myfix" id="whole">
<div class="myfix1"> <h3>Column 1</h3> </div>
<div class="myfix1"> <h3>Column 2</h3> </div>
<div class="myfix1" style="display:none;"> <h3>Column 3</h3> </div>
</div>

</body>
</html>

关于javascript - 在 bootstrap html 中显示/隐藏一个 div 时自动调整 div 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37203257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com