html - 如何创建一个仅在单击/切换时才显示某些输入的按钮?
问题描述
我正在尝试创建完全相同的按钮(视觉下拉)“价格范围”,就像本网站中的那个:https ://dubai.dubizzle.com/en/property-for-rent/residential/apartmentflat/
/* CSS */
.button_wrapper {
background: white none repeat scroll 0% 0%;
border-radius: 6px;
height: 48px;
}
button {
display: flex;
-moz-box-pack: justify;
justify-content: space-between;
-moz-box-align: center;
align-items: center;
height: 100%;
color: black;
font-size: 14px;
box-sizing: border-box;
padding: 0px 6px 0px 12px;
border: 0px none;
width: 100%;
background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;
text-align: left;
cursor: pointer;
}
.lnhPTn {
position: absolute;
background: white none repeat scroll 0% 0%;
color: rgb(182, 184, 185);
margin-top: 4px;
box-sizing: border-box;
border-radius: 6px;
border: 1px solid rgb(43, 45, 46);
height: auto;
padding: 16px;
}
.kFCjgJ {
display: flex;
align-items: flex-end;
-moz-box-pack: center;
justify-content: center;
}
.gacQrk {
font-size: 14px;
font-weight: 600;
line-height: 1.43;
color: rgb(43, 45, 46);
}
.fmQggb {
display: block;
width: 180px;
border-radius: 6px;
border: 1px solid rgb(182, 184, 185);
height: 48px;
padding: 8px 12px;
box-sizing: border-box;
font-size: 14px;
box-shadow: none;
}
..jaNqhG {
border: 0px none;
width: 8px;
margin: 0px 6px 18px;
height: 1px;
background: rgb(43, 45, 46) none repeat scroll 0% 0%;
}
<!-- HTML -->
<!-- BUTTON STARTS HERE -->
<div class="button_wrapper">
<button type="button" class="button_price">
<span class="button_span">Any</span>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M6 9l6 6 6-6"></path>
</svg>
</button>
<div class="lnhPTn">
<div class="bkwgFh">
<div class="kFCjgJ">
<label class="gacQrk" for="min">
Min
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="min">
</label>
<span class="jaNqhG"></span>
<label class="gacQrk" for="max">
Max
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="max">
</label>
</div>
</div>
</div>
</div>
<!-- BUTTON ENDS HERE -->
如何仅在单击/切换按钮时显示按钮下方的 div(包含输入的 div)并在单击/切换其他内容时自动关闭(例如下一个按钮或搜索)?
解决方案
3步中最简单的解决方案-
- 在按钮上添加
onclick="document.querySelector('.lnhPTn').style.display=document.querySelector('.lnhPTn').style.display=='block'?'none':'block'"
属性。 - 添加
<script>
window.onclick=function (e){
var buttonPrice =document.querySelector(".button_price");
var mainDiv=document.querySelector(".lnhPTn");
var inputs=document.querySelectorAll(".lnhPTn input")
console.log(inputs.length)
if(e.target!=buttonPrice && e.target!=mainDiv && e.target!=inputs[0] && e.target!=inputs[1]){
mainDiv.style.display="none";
}
}
</script>
在正文标签的末尾。
- 在 css 中将主 div 的显示设置为无。
所以你的最终代码看起来像
button_wrapper {
background: white none repeat scroll 0% 0%;
border-radius: 6px;
height: 48px;
}
button {
display: flex;
-moz-box-pack: justify;
justify-content: space-between;
-moz-box-align: center;
align-items: center;
height: 100%;
color: black;
font-size: 14px;
box-sizing: border-box;
padding: 0px 6px 0px 12px;
border: 0px none;
width: 100%;
background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;
text-align: left;
cursor: pointer;
}
.lnhPTn {
display:none;
position: absolute;
background: white none repeat scroll 0% 0%;
color: rgb(182, 184, 185);
margin-top: 4px;
box-sizing: border-box;
border-radius: 6px;
border: 1px solid rgb(43, 45, 46);
height: auto;
padding: 16px;
}
.kFCjgJ {
display: flex;
align-items: flex-end;
-moz-box-pack: center;
justify-content: center;
}
.gacQrk {
font-size: 14px;
font-weight: 600;
line-height: 1.43;
color: rgb(43, 45, 46);
}
.fmQggb {
display: block;
width: 180px;
border-radius: 6px;
border: 1px solid rgb(182, 184, 185);
height: 48px;
padding: 8px 12px;
box-sizing: border-box;
font-size: 14px;
box-shadow: none;
}
..jaNqhG {
border: 0px none;
width: 8px;
margin: 0px 6px 18px;
height: 1px;
background: rgb(43, 45, 46) none repeat scroll 0% 0%;
}
<!-- HTML -->
<!-- BUTTON STARTS HERE -->
<div class="button_wrapper">
<button type="button" class="button_price" onclick="document.querySelector('.lnhPTn').style.display=document.querySelector('.lnhPTn').style.display=='block'?'none':'block'">
<span class="button_span">Any</span>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M6 9l6 6 6-6"></path>
</svg>
</button>
<div class="lnhPTn">
<div class="bkwgFh">
<div class="kFCjgJ">
<label class="gacQrk" for="min">
Min
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="min">
</label>
<span class="jaNqhG"></span>
<label class="gacQrk" for="max">
Max
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="max">
</label>
</div>
</div>
</div>
</div>
<!-- BUTTON ENDS HERE -->
<script>
window.onclick=function (e){
var buttonPrice =document.querySelector(".button_price");
var mainDiv=document.querySelector(".lnhPTn");
var inputs=document.querySelectorAll(".lnhPTn input")
if(e.target!=buttonPrice && e.target!=mainDiv && e.target!=inputs[0] && e.target!=inputs[1]){
mainDiv.style.display="none";
}
}
</script>
这是一个带有此代码的工作 jsfiddle https://jsfiddle.net/o48ps293/
推荐阅读
- app-store - 如何填写支持网址字段?
- maven - 调试 JavaFX - 编译失败:无法调用“java.lang.Throwable.getCause()”,因为“原因”为空
- easynetq - EasyNetq 没有收到消息。Rabbitmq 没有收到消息。BackgroundWorker 通过回调函数订阅。但什么也没有发生
- powershell - 在不使用 SupportsShouldProcess 的 cmdlet 中停止 WhatIf 继承
- python - TypeError 在我的输出终端上显示“NoneType 对象不可下标”
- gnuplot - 如何在 Gnuplot 中绘制具有不同索引的两列?
- swiftui - WatchOS Button Group 就像 Timer App 中的?
- mule4 - 如何在 Mule 4 中使用安全的 SOAP Web 服务
- python - 如何避免 .replace 替换已替换的单词
- c - 美元在哪里?保存在bash中?