javascript - 如何强制我的下拉框 (HTML) 在 iOS 滚轮上打开
问题描述
我对编码比较陌生,我很想学习……但是……
我有以下 HTML 下拉框 - 我已经包含了 javascript 和 css 以防万一。
我想要,当用户在 iOS 设备上时,当用户点击下拉菜单时,可以使用 iOS 滚轮选择选项......如下图所示,我希望有人能提供帮助!
代码:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
#myInput {
border-box: box-sizing;
background-image: url('searchicon.png');
background-position: 14px 12px;
background-repeat: no-repeat;
font-size: 16px;
padding: 14px 20px 12px 45px;
border: none;
border-bottom: 1px solid #ddd;
}
#myInput:focus {outline: 3px solid #ddd;}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f6f6f6;
min-width: 250px;
height: 300px;
overflow-y: scroll;
overflow-x: hidden;
border: 1px solid #ddd;
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
<a href="#about">About</a>
<a href="#base">Base</a>
<a href="#blog">Blog</a>
<a href="#contact">Contact</a>
<a href="#custom">Custom</a>
<a href="#support">Support</a>
<a href="#tools">Tools</a>
</div>
</div>
解决方案
欢迎来到本站!
我认为您的问题是您已经实现了自己的下拉菜单,而不是使用内置和标签。我相信使用这些会导致 iOS 在与该元素交互时自动呈现选择器。
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("dropdown-block").classList.toggle("show");
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("option");
for (i = 0; i < a.length; i++) {
if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
#myInput {
border-box: box-sizing;
background-image: url('searchicon.png');
background-position: 14px 12px;
background-repeat: no-repeat;
font-size: 16px;
padding: 14px 20px 12px 45px;
border: none;
border-bottom: 1px solid #ddd;
}
#myInput:focus {outline: 3px solid #ddd;}
.dropdown {
position: relative;
display: none;
}
.dropdown-content {
background-color: #f6f6f6;
min-width: 250px;
overflow-y: scroll;
overflow-x: hidden;
border: 1px solid #ddd;
z-index: 1;
display:block;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="dropdown-block" class="dropdown">
<input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
<select id="myDropdown" class="dropdown-content">
<option href="#about">About</option>
<option href="#base">Base</option>
<option href="#blog">Blog</option>
<option href="#contact">Contact</option>
<option href="#custom">Custom</option>
<option href="#support">Support</option>
<option href="#tools">Tools</option>
</select>
</div>
推荐阅读
- javascript - 如何通过javascript更改类的所有元素的字体颜色?
- node.js - 垃圾邮件套接字 io 连接
- codeigniter - 重定向在真实服务器上的 Codeigniter 中不起作用。错误报告但未显示任何错误
- typescript - 为什么不分配它,尽管 Array 是协变的?
- c++ - 带有 shared_ptr 的处理程序中的分段错误
- excel - 带字符串的公式
- azure - 当应用程序在 docker 上运行时,Windows Docker 容器 > 呈现的 PDF 的布局不同
- laravel - 如何安装/使用这个包 = akiyamaSM / larapoll
- python - 用于工作 python 脚本的多输入 PySimpleGUI
- entity-framework - 如何找到财产的持有人