<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 下拉菜单选择器</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
}
.dropdown-container {
margin: 20px 0;
}
.custom-dropdown {
position: relative;
width: 100%;
}
.dropdown-select {
width: 100%;
padding: 12px;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 4px;
background-color: white;
cursor: pointer;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
background-image: url('data:image/svg+xml;utf8,<svg fill="%23333" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/></svg>');
background-repeat: no-repeat;
background-position: right 10px center;
}
.dropdown-select:focus {
outline: none;
border-color: #4CAF50;
box-shadow: 0 0 5px rgba(76, 175, 80, 0.5);
}
.dropdown-options {
display: none;
position: absolute;
top: 100%;
left: 0;
right: 0;
background-color: white;
border: 1px solid #ddd;
border-top: none;
border-radius: 0 0 4px 4px;
max-height: 200px;
overflow-y: auto;
z-index: 1000;
}
.dropdown-options.show {
display: block;
}
.dropdown-option {
padding: 10px 15px;
cursor: pointer;
transition: background-color 0.2s;
}
.dropdown-option:hover {
background-color: #f0f0f0;
}
.dropdown-option.selected {
background-color: #e0e0e0;
}
.result {
margin-top: 20px;
padding: 15px;
background-color: #f9f9f9;
border-radius: 4px;
border-left: 4px solid #4CAF50;
}
.footer {
margin-top: 30px;
text-align: center;
color: #666;
font-size: 14px;
}
.footer a {
color: #4CAF50;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>jQuery 下拉菜单选择器</h1>
<div class="dropdown-container">
<div class="custom-dropdown">
<select id="fruit-select" class="dropdown-select">
<option value="">请选择水果</option>
<option value="apple">苹果</option>
<option value="banana">香蕉</option>
<option value="orange">橙子</option>
<option value="grape">葡萄</option>
<option value="watermelon">西瓜</option>
<option value="strawberry">草莓</option>
</select>
<div id="fruit-options" class="dropdown-options"></div>
</div>
</div>
<div class="result">
<p>您选择的水果是: <span id="selected-fruit">无</span></p>
</div>
<div class="footer">
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 初始化下拉选项
function initDropdownOptions() {
const $select = $('#fruit-select');
const $optionsContainer = $('#fruit-options');
// 清空现有选项
$optionsContainer.empty();
// 添加选项
$select.find('option').each(function() {
if ($(this).val() !== '') {
const $option = $('<div class="dropdown-option" data-value="' + $(this).val() + '">' + $(this).text() + '</div>');
$optionsContainer.append($option);
}
});
// 设置当前选中的选项
const selectedValue = $select.val();
if (selectedValue) {
$optionsContainer.find('.dropdown-option[data-value="' + selectedValue + '"]').addClass('selected');
$('#selected-fruit').text($select.find('option:selected').text());
}
}
// 初始化
initDropdownOptions();
// 点击选择框显示/隐藏选项
$('.dropdown-select').on('click', function(e) {
e.stopPropagation();
$(this).next('.dropdown-options').toggleClass('show');
});
// 点击选项
$(document).on('click', '.dropdown-option', function() {
const value = $(this).data('value');
const text = $(this).text();
// 更新选择框
$('#fruit-select').val(value);
// 更新显示
$('#selected-fruit').text(text);
// 更新选中状态
$('.dropdown-option').removeClass('selected');
$(this).addClass('selected');
// 隐藏选项
$('.dropdown-options').removeClass('show');
});
// 点击其他地方隐藏选项
$(document).on('click', function() {
$('.dropdown-options').removeClass('show');
});
// 阻止事件冒泡
$('.dropdown-options').on('click', function(e) {
e.stopPropagation();
});
// 键盘导航
$('.dropdown-select').on('keydown', function(e) {
const $options = $(this).next('.dropdown-options');
const $visibleOptions = $options.find('.dropdown-option:visible');
const $selected = $options.find('.dropdown-option.selected');
if (e.keyCode === 40) { // 向下箭头
e.preventDefault();
if (!$options.hasClass('show')) {
$options.addClass('show');
} else {
let next = $selected.length ? $selected.next('.dropdown-option') : $visibleOptions.first();
if (next.length) {
$selected.removeClass('selected');
next.addClass('selected');
next[0].scrollIntoView({ block: 'nearest' });
}
}
} else if (e.keyCode === 38) { // 向上箭头
e.preventDefault();
let prev = $selected.length ? $selected.prev('.dropdown-option') : $visibleOptions.last();
if (prev.length) {
$selected.removeClass('selected');
prev.addClass('selected');
prev[0].scrollIntoView({ block: 'nearest' });
}
} else if (e.keyCode === 13) { // 回车
e.preventDefault();
if ($options.hasClass('show') && $selected.length) {
$selected.trigger('click');
} else {
$options.toggleClass('show');
}
} else if (e.keyCode === 27) { // ESC
e.preventDefault();
$options.removeClass('show');
}
});
});
</script>
</body>
</html>
2762

被折叠的 条评论
为什么被折叠?



