本文通过实例代码给大家介绍了jquery 与css相结合实现的下拉列表功能,非常不错,具有参考借鉴价值,需要的朋友参考下吧,希望能帮助到大家。
废话不多说了,直接给大家贴代码了,具体代码如下所述:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fruit</title>
<style type="text/css">
.hide {
display: none;
}
p {
float: left;
width: 100%;
}
.selector-containter {
margin-bottom: 10px;
}
.selector {
width: 200px;
background: #FFF;
border: 1px solid #DDD;
}
.selector-hint {
width: 178px;
border: 1px solid #DDD;
}
.selector-expand {
width: 8px;
border: 1px solid #DDD;
}
.selector-collapse {
width: 8px;
border: 1px solid #DDD;
}
</style>
<script src="jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function() {
//使用on方法,采用事件委派机制,selector-option-container中的内容为后续动态追加
$('.selector').on('click', '.selector-expand', function() {
$(this).parent().children('.selector-option-container').children().remove();
$(this).parent().children('.selector-option-container').append('<p><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></p><p class="selector-option">apricot</p>');
$(this).parent().children('.selector-option-container').append('<p><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></p><p class="selector-option">banana</p>');
$(this).nextAll('.selector-option-container').removeClass('hide');
});
$('.selector').on('click', '.selector-collapse', function() {
$(this).nextAll('.selector-option-container').addClass('hide');
});
$('.selector-t1').on('click', '.selector-option', function() {
$(this).parent().parent().children('.selector-hint').text($(this).text());
$(this).parent().addClass('hide');
});
$('.selector-t1').on('click', '.selector-checkbox', function() {
$(this).parent().parent().parent().children('.selector-hint').text($(this).parent().next().text());
//采用prop方法,对于值为布尔型的属性赋值
$(this).prop('checked', false);
$(this).parent().parent().addClass('hide');
});
});
</script>
</head>
<body>
<p id="titan" class="selector-containter">
<p id="fruit">
<p class="selector">
<p class="selector-hint">select fruit</p>
<p class="selector-expand">+</p>
<p class="selector-collapse">-</p>
<p class="selector-option-container">
</p>
</p>
</p>
</p>
<p id="athena" class="selector-t1 selector-containter">
<p id="fruit">
<p class="selector">
<p class="selector-hint">select fruit</p>
<p class="selector-expand">+</p>
<p class="selector-collapse">-</p>
<p class="selector-option-container">
</p>
</p>
</p>
</p>
</body>
</html>相关推荐:
关于Vue.js表单标签中的单选按钮、复选按钮和下拉列表的取值详解
立即学习“前端免费学习笔记(深入)”;
以上就是关于jquery+css实现下拉列表功能的实例详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号