gpt4 book ai didi

c# - 如何在不占用空间的情况下使用 CSS 包含隐藏的 DIV?

转载 作者:行者123 更新时间:2023-11-30 20:49:41 24 4
gpt4 key购买 nike

我有一个带有单个下拉列表的简单 MVC 5 页面。根据从此下拉列表中所做的选择,我启用了页面上三个 div 之一的可见性。我遇到的问题是每个 div 都会占用页面上的空间,即使它不可见也是如此。因此,当我从下拉列表中选择使第二个 div 可见的内容时,我会看到该内容在页面上向下移动。

下面是来自 Controller 的代码,用于为下拉列表创建数据。

public ActionResult Index()
{
var searchBy = new List<SearchBy>
{
new SearchBy { Name = "Email Address", Value = "EmailAddress" },
new SearchBy { Name = "Last name, First name", Value = "Name" },
new SearchBy { Name = "Username", Value = "Username" }
};

ViewBag.SearchByOptions = new SelectList(searchBy, "Value", "Name");

return View();
}

这是我对 Index.cshtml 的标记

@{
<script type="text/javascript">
$(document).ready(function() {
// Make all three <div>s hidden when the page loads...
document.getElementById("searchByEmail").style.visibility = "hidden";
document.getElementById("searchByName").style.visibility = "hidden";
document.getElementById("searchByUsername").style.visibility = "hidden";
});

function searchBy(selectedItem) {
if (selectedItem == "EmailAddress") {
// Make visible
document.getElementById("searchByEmail").style.visibility = "visible";
// Make in-visible
document.getElementById("searchByName").style.visibility = "hidden";
document.getElementById("searchByUsername").style.visibility = "hidden";
}

if (selectedItem == "Name") {
// Make visible
document.getElementById("searchByName").style.visibility = "visible";
// Make in-visible
document.getElementById("searchByEmail").style.visibility = "hidden";
document.getElementById("searchByUsername").style.visibility = "hidden";
}

if (selectedItem == "Username") {
// Make visible
document.getElementById("searchByUsername").style.visibility = "visible";
// Make in-visible
document.getElementById("searchByEmail").style.visibility = "hidden";
document.getElementById("searchByName").style.visibility = "hidden";
}
};
</script>
}

<h2>Index</h2>
<div>
Search for existing users by: @Html.DropDownList("SelectedItem", (SelectList)ViewBag.SearchByOptions, "-- Select One --", new { onchange = "searchBy($('#SelectedItem').val());" })
</div>

<div id="searchByEmail">
Emails...
</div>

<div id="searchByName">
Names...
</div>

<div id="searchByUsername">
Usernames...
</div>
}

我不确定需要什么技巧才能让所有 div 在页面上占用相同的“不动产”,因为我一次只显示其中一个。

enter image description here enter image description here enter image description here

最佳答案

假设您使用 jQuery,请尝试:

@{
<script type="text/javascript">
$(document).ready(function() {
$("#searchByEmail").hide();
$("#searchByName").hide();
$("#searchByUsername").hide();
});

function searchBy(selectedItem) {
if (selectedItem == "EmailAddress") {
$("#searchByEmail").show();
$("#searchByName").hide();
$("#searchByUsername").hide();
}
if (selectedItem == "Name") {
$("#searchByName").show();
$("#searchByEmail").hide();
$("#searchByUsername").hide();
}

if (selectedItem == "Username") {
$("#searchByUsername").show();
$("#searchByEmail").hide();
$("#searchByName").hide();
}
};
</script>
}

<h2>Index</h2>
<div>
Search for existing users by: @Html.DropDownList("SelectedItem", (SelectList)ViewBag.SearchByOptions, "-- Select One --", new { onchange = "searchBy($('#SelectedItem').val());" })
</div>

此外,检查 CSS 规则之间的区别:

visibility:hidden

display:none

第一个只是隐藏元素,但保留占位符与它可见时相同的大小。第二个将其从大小和尺寸计算中删除。

关于c# - 如何在不占用空间的情况下使用 CSS 包含隐藏的 DIV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23295414/

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