這篇文章主要介紹jquery+css如何實現下拉列表功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體代碼如下所述:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fruit</title>
<style type="text/css">
.hide {
display: none;
}
div {
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('<div><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></div><div class="selector-option">apricot</div>');
$(this).parent().children('.selector-option-container').append('<div><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></div><div class="selector-option">banana</div>');
$(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>
<div id="titan" class="selector-containter">
<div id="fruit">
<div class="selector">
<div class="selector-hint">select fruit</div>
<div class="selector-expand">+</div>
<div class="selector-collapse">-</div>
<div class="selector-option-container">
</div>
</div>
</div>
</div>
<div id="athena" class="selector-t1 selector-containter">
<div id="fruit">
<div class="selector">
<div class="selector-hint">select fruit</div>
<div class="selector-expand">+</div>
<div class="selector-collapse">-</div>
<div class="selector-option-container">
</div>
</div>
</div>
</div>
</body>
</html>以上是“jquery+css如何實現下拉列表功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。