function restoreMenu(selectId) { var select = document.getElementById(selectId); select.style.display = 'inline-block'; select.selectedIndex = -1; var span = select.parentNode.getElementsByTagName('span')[0]; select.parentNode.removeChild(span); }
时间: 2023-06-18 10:03:14 浏览: 48
这是一个 JavaScript 函数,用于恢复下拉菜单(select)的初始状态。具体来说,它会将下拉菜单的选中项设置为 -1,同时将下拉菜单的 display 样式设置为 inline-block。此外,它还会移除下拉菜单的父元素中的第一个 span 元素。
需要传递一个参数 selectId,表示要恢复的下拉菜单的 id。
相关问题
.input-group { margin-bottom: 10px; } .input-group label { display: block; margin-bottom: 5px; } .input-group input { width: 100px; padding: 5px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } .input-group select { width: 100px; padding: 5px; border: 1px solid #B0E0E6; border-radius: 4px; box-sizing: border-box; } .login-btn { width: 100%; height: 10px; background-color: #aaffaa; color: #fff; padding: 30px 25px; border-radius: 4px; cursor: pointer; float: right; box-sizing: border-box; } .container div input:focus { background-color: #ffe4e1; } .login-btn:hover { background-color: #45a049; } </style> <script> function showAdminInput() { var userType = document.getElementById("user-type").value; if (userType == "admin") { document.getElementById("username-label").innerHTML = "管理员用户名:"; } else { document.getElementById("username-label").innerHTML = "请输入用户名:"; } } function login() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; var userType = document.getElementById("user-type").value; } </script> </head> <body style="background-image: url(image/bk8.gif); background-size: cover; background-repeat: no-repeat;color: pink;"> <div class="container" > <h1 style="font-size: 40px; text-align: center; text-color: #B0E0E6">Login</h1> <div class="input-group" > <label id="username-label">请输入用户名:</label> <input type="text" id="username" name="username" style="border-bottom: 1px solid black;padding-bottom: 2px;display:inline-block;"> </div> <div class="input-group" > <label>请你输入密码:</label> <input type="password" id="password" name="password" style="border-bottom: 1px solid black;padding-bottom: 2px;display:inline-block;"> </div> <div class="input-group" > <label>选择用户类型:</label> <select id="user-type" name="user-type" onchange="showAdminInput()" style="color: blue;"> <option value="normal">普通用户</option> <option value="admin">管理员</option> </select> </div> <button class="login-btn" onclick="login()" >GO</button> </div>为什么标签和输入框不能在同一行
标签和输入框不能在同一行是因为它们都被包含在一个class为“input-group”的div元素中,而该div元素默认的display属性是block,即块级元素,会自动换行。如果想让它们在同一行,可以将该div元素的display属性设置为inline-block或者使用CSS的flexbox布局。例如可以在.input-group样式中添加"display: inline-block;"。
用HTML+css+js写个select选择器
下面是一个简单的 HTML+CSS+JS 实现的 select 选择器示例:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Select</title>
<style>
/* 隐藏原生 select */
select {
display: none;
}
/* 外层容器 */
.custom-select {
position: relative;
display: inline-block;
}
/* 下拉箭头 */
.custom-select::after {
content: "\25BC";
font-size: 12px;
color: gray;
position: absolute;
top: 50%;
right: 12px;
transform: translateY(-50%);
}
/* 显示框 */
.custom-select-selected {
display: inline-block;
padding: 6px 12px;
border: 1px solid gray;
border-radius: 4px;
background-color: #fff;
cursor: pointer;
}
/* 下拉选项容器 */
.custom-select-items {
position: absolute;
z-index: 1;
top: 100%;
left: 0;
right: 0;
background-color: #fff;
border: 1px solid gray;
border-top: none;
border-radius: 0 0 4px 4px;
overflow-y: auto;
max-height: 200px;
}
/* 下拉选项 */
.custom-select-item {
display: block;
padding: 6px 12px;
cursor: pointer;
}
/* 鼠标悬停样式 */
.custom-select-item:hover {
background-color: #f1f1f1;
}
/* 选中样式 */
.custom-select-item.selected {
background-color: #ddd;
}
</style>
</head>
<body>
<div class="custom-select">
<select id="my-select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
<option value="option5">Option 5</option>
</select>
<div class="custom-select-selected">Option 1</div>
<div class="custom-select-items">
<div class="custom-select-item" data-value="option1">Option 1</div>
<div class="custom-select-item" data-value="option2">Option 2</div>
<div class="custom-select-item" data-value="option3">Option 3</div>
<div class="custom-select-item" data-value="option4">Option 4</div>
<div class="custom-select-item" data-value="option5">Option 5</div>
</div>
</div>
<script>
var select = document.getElementById("my-select");
var selectContainer = document.querySelector(".custom-select");
var selectSelected = selectContainer.querySelector(".custom-select-selected");
var selectItemsContainer = selectContainer.querySelector(".custom-select-items");
var selectItems = selectItemsContainer.querySelectorAll(".custom-select-item");
// 初始化选中值
selectSelected.textContent = select.options[select.selectedIndex].textContent;
// 点击显示下拉选项
selectSelected.addEventListener("click", function () {
selectItemsContainer.classList.toggle("show");
});
// 点击选项
for (var i = 0; i < selectItems.length; i++) {
selectItems[i].addEventListener("click", function () {
selectSelected.textContent = this.textContent;
select.value = this.dataset.value;
for (var j = 0; j < selectItems.length; j++) {
selectItems[j].classList.remove("selected");
}
this.classList.add("selected");
selectItemsContainer.classList.remove("show");
});
}
// 点击其他地方隐藏下拉选项
window.addEventListener("click", function (e) {
if (!selectContainer.contains(e.target)) {
selectItemsContainer.classList.remove("show");
}
});
</script>
</body>
</html>
```
这个示例使用了 CSS 伪元素和 JS 事件处理来实现一个自定义的 select 选择器。在 HTML 中,我们隐藏了原生的 select 元素,使用 div 和 ul/li 元素代替。当点击显示框时,下拉选项会显示出来,并且选中的选项会更新到显示框中。当点击其他地方时,下拉选项会隐藏。你可以根据自己的需要修改样式和功能。