Html 如何制作一个下拉列表来响应您输入的字母?
Html 如何制作一个下拉列表来响应您输入的字母?,html,xml,jsp,Html,Xml,Jsp,选择一个国家 你想去哪里? 美国 土耳其 法国 西班牙 埃及 迪拜 要对齐页面中心的文本,只需向表单中添加一个类,如下所示: <form action="some.jsp" class="form"> 默认情况下,如果选项列表处于打开状态,并且您按下了一个字母,则带有该字母的第一个选项将被聚焦 如果要对列表进行排序/筛选,则必须使用javascript。然后我建议使用和元素 但这里有一个简单的列表过滤和css类。对于少量的元素来说效果很好。如果您有很多元素,请考虑使用数组.Fiel
选择一个国家
你想去哪里?
美国
土耳其
法国
西班牙
埃及
迪拜
要对齐页面中心的文本,只需向表单中添加一个类,如下所示:
<form action="some.jsp" class="form">
默认情况下,如果选项列表处于打开状态,并且您按下了一个字母,则带有该字母的第一个选项将被聚焦
如果要对列表进行排序/筛选,则必须使用javascript。然后我建议使用
和
元素
但这里有一个简单的列表过滤和css类。对于少量的元素来说效果很好。如果您有很多元素,请考虑使用<代码>数组.Fielter()/<代码>而不是<代码>数组.MaP()/代码>和<代码>元素.CaseList.P/>
<body>
<style>
.option--hidden {
display: none;
}
</style>
<ul class="my-list">
<li>AA</li>
<li>AB</li>
<li>AC</li>
<li>BA</li>
<li>BB</li>
<li>BC</li>
<li>CA</li>
<li>CB</li>
<li>CC</li>
</ul>
<script>
//
// I like to use an instance variable to store data in order to avoid unneccessary loading
//
instance = {
options: [...document.querySelectorAll('.my-list li')],
}
function hideAllButSelected(pressedKey) {
//
// Loop through options ans see if their first characters match the pressed key.
// If yes then leave it visible, otherwise hide them.
//
instance.options.map(option => {
const optionText = option.innerText.toLowerCase();
const optionFirstCharacter = optionText.charAt(0).toLowerCase();
// You can freely call classList.remove() because it gives no error even if classname doesn't exist.
// On every iteration remove the hidden -class to show all that are not in the scope of pressed key.
option.classList.remove('option--hidden');
if (pressedKey !== optionFirstCharacter) option.classList.add('option--hidden');
});
}
function showAllOptions() {
instance.options.map(option => {
option.classList.remove('option--hidden');
});
}
// Pls use a handler function for events.
function handleKeyPress(e) {
const key = e.key;
if (key === '§') {
this.showAllOptions();
} else {
this.hideAllButSelected(key);
}
}
document.addEventListener('keypress', e => this.handleKeyPress(e));
</script>
.选项--隐藏{
显示:无;
}
- AA
- AB
- 交流电
- BA
- BB
- 卑诗省
- CA
- CB
- 抄送
//
//我喜欢使用实例变量来存储数据,以避免不必要的加载
//
实例={
选项:[…document.querySelectorAll('.my list li')],
}
功能HIDEALBUTTSELECTED(按键){
//
//循环浏览选项并查看其第一个字符是否与按下的键匹配。
//如果是,则使其可见,否则将其隐藏。
//
instance.options.map(option=>{
const optionText=option.innerText.toLowerCase();
常量optionFirstCharacter=optionText.charAt(0.toLowerCase();
//您可以自由调用classList.remove(),因为即使classname不存在,它也不会给出错误。
//在每次迭代中,删除隐藏类以显示所有不在按下键范围内的内容。
option.classList.remove('option--hidden');
if(按键!==optionFirstCharacter)option.classList.add('option--hidden');
});
}
函数showAllOptions(){
instance.options.map(option=>{
option.classList.remove('option--hidden');
});
}
//请使用事件处理函数。
功能手柄按键(e){
const key=e.key;
如果(键=='§'){
这是showaloptions();
}否则{
此.hidealbutselected(键);
}
}
document.addEventListener('keypress',e=>this.handleKeyPress(e));
我不明白你想用下拉列表做什么,它是一个过滤器?尝试一些自动完成文本,这将对你有所帮助。请不要通过破坏你的帖子为其他人做更多的工作。通过在Stack Exchange网络上发布,您已授予Stack Exchange在下不可撤销的权利,以分发该内容(即,无论您未来的选择如何)。根据堆栈交换策略,帖子的非破坏版本是分发的版本。因此,任何故意破坏行为都将恢复原状。如果你想了解更多关于删除帖子的信息,请参阅:这个问题的答案是什么?“我想把这个与页面的中心对齐。我该怎么做。”问题的这部分?你没有回答主要部分,我添加了一条评论,因为我没有得到主要部分,如果他想要一个过滤器或其他东西…好的,我会解释更新答案。OP需要类似的自动完成文本行为
<body>
<style>
.option--hidden {
display: none;
}
</style>
<ul class="my-list">
<li>AA</li>
<li>AB</li>
<li>AC</li>
<li>BA</li>
<li>BB</li>
<li>BC</li>
<li>CA</li>
<li>CB</li>
<li>CC</li>
</ul>
<script>
//
// I like to use an instance variable to store data in order to avoid unneccessary loading
//
instance = {
options: [...document.querySelectorAll('.my-list li')],
}
function hideAllButSelected(pressedKey) {
//
// Loop through options ans see if their first characters match the pressed key.
// If yes then leave it visible, otherwise hide them.
//
instance.options.map(option => {
const optionText = option.innerText.toLowerCase();
const optionFirstCharacter = optionText.charAt(0).toLowerCase();
// You can freely call classList.remove() because it gives no error even if classname doesn't exist.
// On every iteration remove the hidden -class to show all that are not in the scope of pressed key.
option.classList.remove('option--hidden');
if (pressedKey !== optionFirstCharacter) option.classList.add('option--hidden');
});
}
function showAllOptions() {
instance.options.map(option => {
option.classList.remove('option--hidden');
});
}
// Pls use a handler function for events.
function handleKeyPress(e) {
const key = e.key;
if (key === '§') {
this.showAllOptions();
} else {
this.hideAllButSelected(key);
}
}
document.addEventListener('keypress', e => this.handleKeyPress(e));
</script>