gpt4 book ai didi

javascript - 页面元素缩进

转载 作者:行者123 更新时间:2023-11-28 11:18:49 28 4
gpt4 key购买 nike

我有一个表单,其中某些元素根据选择框显示/隐藏。那部分对我来说很完美。我遇到的问题是,最初隐藏的部分内的元素在显示时缩进了,我不知道为什么。我确信这与我的 CSS 有关系。我尝试将 .register form section input... 添加到 .register form input... 中,但这没有任何区别。任何帮助将不胜感激。

$('section').hide()
$('#user_level').change(function() {
var value = this.value;
$('section').hide()
$('#' + this.value).show();
});
* {
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "segoe ui", roboto, oxygen, ubuntu, cantarell, "fira sans", "droid sans", "helvetica neue", Arial, sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

body {
background-color: #f3f4f7;
}

body.loggedin {
background-color: #f3f4f7;
}

.register {
width: 400px;
background-color: #ffffff;
box-shadow: 0 0 9px 0 rgba(0, 0, 0, 0.3);
margin: 100px auto;
padding: 0px;
}

.register h1 {
text-align: center;
color: #5b6574;
font-size: 24px;
padding: 20px 0 20px 0;
border-bottom: 1px solid #dee0e4;
}

.register form {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding-top: 20px;
}

.register form label {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
background-color: #3a4b3b;
color: #ffffff;
}

.register form input[type="password"],
.register form input[type="text"],
.register form input[type="email"],
.register form select {
width: 310px;
height: 50px;
border: 1px solid #dee0e4;
margin-bottom: 20px;
padding: 0 15px;
}

.register form input[type="submit"] {
width: 100%;
padding: 15px;
margin-top: 20px;
background-color: #3a4b3b;
border: 0;
cursor: pointer;
font-weight: bold;
color: #ffffff;
transition: background-color 0.2s;
}

.register form input[type="submit"]:hover {
background-color: #537a56;
transition: background-color 0.2s;
}

.dropdown {
float: left;
overflow: hidden;
}

.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: #3a4b3b;
font-family: inherit;
margin: 0;
}

.dropdown:hover .dropbtn {
color: #eaebed;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}

.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown-content a:hover {
background-color: #ddd;
color: black;
}

.dropdown:hover .dropdown-content {
display: block;
}

.select-css {
display: block;
color: #666;
line-height: 1.3;
padding: .6em 1.4em .5em .8em;
width: 100%;
max-width: 100%;
box-sizing: border-box;
margin: 0;
border: 1px solid #aaa;
box-shadow: 0 1px 0 1px rgba(0, 0, 0, .04);
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
background-color: #fff;
background-image: url('data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23007CB2%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E'), linear-gradient(to bottom, #ffffff 0%, #e5e5e5 100%);
background-repeat: no-repeat, repeat;
background-position: right .7em top 50%, 0 0;
background-size: .65em auto, 100%;
}

.select-css::-ms-expand {
display: none;
}

.select-css:hover {
border-color: #888;
}

.select-css:focus {
border-color: #aaa;
box-shadow: 0 0 1px 3px rgba(59, 153, 252, .7);
box-shadow: 0 0 0 3px -moz-mac-focusring;
color: #222;
outline: none;
}

.select-css option {
font-weight: normal;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<div class="register">
<h1>Add User</h1>
<form action="#" method="post" autocomplete="off">
<label for="first_name"><i class="fas fa-user"></i></label><input type="text" name="first_name" id="first_name" placeholder="First Name">
<label for="last_name"><i class="fas fa-user"></i></label><input type="text" name="last_name" id="last_name" placeholder="Last Name">
<label for="email"><i class="fas fa-envelope"></i></label><input type="text" name="email" id="email" placeholder="Email">
<label for="user_level"><i class="fas fa-user-tag"></i></label>
<select class="select-css" name="user_level" id="user_level">
<option value="0" selected disabled>User Type</option>
<option value="1">Student</option>
<option value="2">Faculty/Staff</option>
<option value="3">System User</option>
<option value="4">System Administrator</option>
</select>
<section id="1">
<label for="grade"><i class="fas fa-envelope"></i></label><input type="text" name="grade" id="grade" placeholder="Grade">
</section>
<section id="2">
<label for="division"><i class="fas fa-building"></i></label>
<select class="select-css" name="division" id="division">
<option value="" selected disabled>Division</option>
<option value="1">Central Office</option>
<option value="2">Elementary School</option>
<option value="3">Middle School</option>
<option value="4">High School</option>
</select>
</section>
<section id="3">
<label for="division"><i class="fas fa-building"></i></label>
<select class="select-css" name="division" id="division">
<option value="" selected disabled>Division</option>
<option value="1">Central Office</option>
<option value="2">Elementary School</option>
<option value="3">Middle School</option>
<option value="4">High School</option>
</select>
<label for="user_name"><i class="fas fa-user"></i></label><input type="text" name="user_name" id="user_name" placeholder="User Name">
</section>
<section id="4">
<label for="division"><i class="fas fa-building"></i></label>
<select class="select-css" name="division" id="division">
<option value="" selected disabled>Division</option>
<option value="1">Central Office</option>
<option value="2">Elementary School</option>
<option value="3">Middle School</option>
<option value="4">High School</option>
</select>
<label for="user_name"><i class="fas fa-user"></i></label><input type="text" name="user_name" id="user_name" placeholder="User Name">
</section>
<input type="submit" value="Add User">
</form>
</div>

最佳答案

问题是因为当您调用 show() 时,它会在 section 元素上设置 display: block。这是一个问题,因为它们需要按照 HTML 结构的其余部分进行 display: flex

要解决此问题,请创建一个 CSS 类来设置显示设置,然后使用 addClass()removeClass() 代替 hide()show():

$('#user_level').change(function() {
$('section').removeClass('show')
$('#' + this.value).addClass('show');
});
section {
display: none;
}

section.show {
display: flex;
}

这是完整的工作示例:

$('#user_level').change(function() {
$('section').removeClass('show')
$('#' + this.value).addClass('show');
});
* {
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "segoe ui", roboto, oxygen, ubuntu, cantarell, "fira sans", "droid sans", "helvetica neue", Arial, sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

body {
background-color: #f3f4f7;
}

body.loggedin {
background-color: #f3f4f7;
}

.register {
width: 400px;
background-color: #ffffff;
box-shadow: 0 0 9px 0 rgba(0, 0, 0, 0.3);
margin: 100px auto;
padding: 0px;
}

.register h1 {
text-align: center;
color: #5b6574;
font-size: 24px;
padding: 20px 0 20px 0;
border-bottom: 1px solid #dee0e4;
}

.register form {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding-top: 20px;
}

.register form label {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
background-color: #3a4b3b;
color: #ffffff;
}

.register form input[type="password"],
.register form input[type="text"],
.register form input[type="email"],
.register form select {
width: 310px;
height: 50px;
border: 1px solid #dee0e4;
margin-bottom: 20px;
padding: 0 15px;
}

.register form input[type="submit"] {
width: 100%;
padding: 15px;
margin-top: 20px;
background-color: #3a4b3b;
border: 0;
cursor: pointer;
font-weight: bold;
color: #ffffff;
transition: background-color 0.2s;
}

.register form input[type="submit"]:hover {
background-color: #537a56;
transition: background-color 0.2s;
}

.dropdown {
float: left;
overflow: hidden;
}

.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: #3a4b3b;
font-family: inherit;
margin: 0;
}

.dropdown:hover .dropbtn {
color: #eaebed;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}

.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown-content a:hover {
background-color: #ddd;
color: black;
}

.dropdown:hover .dropdown-content {
display: block;
}

.select-css {
display: block;
color: #666;
line-height: 1.3;
padding: .6em 1.4em .5em .8em;
width: 100%;
max-width: 100%;
box-sizing: border-box;
margin: 0;
border: 1px solid #aaa;
box-shadow: 0 1px 0 1px rgba(0, 0, 0, .04);
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
background-color: #fff;
background-image: url('data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23007CB2%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E'), linear-gradient(to bottom, #ffffff 0%, #e5e5e5 100%);
background-repeat: no-repeat, repeat;
background-position: right .7em top 50%, 0 0;
background-size: .65em auto, 100%;
}

.select-css::-ms-expand {
display: none;
}

.select-css:hover {
border-color: #888;
}

.select-css:focus {
border-color: #aaa;
box-shadow: 0 0 1px 3px rgba(59, 153, 252, .7);
box-shadow: 0 0 0 3px -moz-mac-focusring;
color: #222;
outline: none;
}

.select-css option {
font-weight: normal;
}

section {
display: none;
}
section.show {
display: flex;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<div class="register">
<h1>Add User</h1>
<form action="#" method="post" autocomplete="off">
<label for="first_name"><i class="fas fa-user"></i></label><input type="text" name="first_name" id="first_name" placeholder="First Name">
<label for="last_name"><i class="fas fa-user"></i></label><input type="text" name="last_name" id="last_name" placeholder="Last Name">
<label for="email"><i class="fas fa-envelope"></i></label><input type="text" name="email" id="email" placeholder="Email">
<label for="user_level"><i class="fas fa-user-tag"></i></label>
<select class="select-css" name="user_level" id="user_level">
<option value="0" selected disabled>User Type</option>
<option value="1">Student</option>
<option value="2">Faculty/Staff</option>
<option value="3">System User</option>
<option value="4">System Administrator</option>
</select>
<section id="1">
<label for="grade"><i class="fas fa-envelope"></i></label><input type="text" name="grade" id="grade" placeholder="Grade">
</section>
<section id="2">
<label for="division"><i class="fas fa-building"></i></label>
<select class="select-css" name="division" id="division">
<option value="" selected disabled>Division</option>
<option value="1">Central Office</option>
<option value="2">Elementary School</option>
<option value="3">Middle School</option>
<option value="4">High School</option>
</select>
</section>
<section id="3">
<label for="division"><i class="fas fa-building"></i></label>
<select class="select-css" name="division" id="division">
<option value="" selected disabled>Division</option>
<option value="1">Central Office</option>
<option value="2">Elementary School</option>
<option value="3">Middle School</option>
<option value="4">High School</option>
</select>
<label for="user_name"><i class="fas fa-user"></i></label><input type="text" name="user_name" id="user_name" placeholder="User Name">
</section>
<section id="4">
<label for="division"><i class="fas fa-building"></i></label>
<select class="select-css" name="division" id="division">
<option value="" selected disabled>Division</option>
<option value="1">Central Office</option>
<option value="2">Elementary School</option>
<option value="3">Middle School</option>
<option value="4">High School</option>
</select>
<label for="user_name"><i class="fas fa-user"></i></label><input type="text" name="user_name" id="user_name" placeholder="User Name">
</section>
<input type="submit" value="Add User">
</form>
</div>

作为旁注,如果在页面加载时应隐藏一组元素,请始终将它们设置为隐藏在 CSS 中。在 JS 中执行意味着它们可能会在页面加载时短时间可见,然后突然消失。

关于javascript - 页面元素缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59200182/

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