【发布时间】:2017-02-06 20:52:42
【问题描述】:
jQuery(document).ready(function (e) {
function t(t) {
e(t).bind("click", function (t) {
t.preventDefault();
e(this).parent().fadeOut()
})
}
e(".dropdown-toggle").click(function () {
var t = e(this).parents(".button-dropdown").children(".dropdown-menu").is(":hidden");
e(".button-dropdown .dropdown-menu").hide();
e(".button-dropdown .dropdown-toggle").removeClass("active");
if (t) {
e(this).parents(".button-dropdown").children(".dropdown-menu").toggle().parents(".button-dropdown").children(".dropdown-toggle").addClass("active");
}
});
e(document).bind("click", function (t) {
var n = e(t.target);
if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-menu").hide();
});
e(document).bind("click", function (t) {
var n = e(t.target);
if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-toggle").removeClass("active");
})
});
.button-dropdown {
position: relative;
top: 5px;
width: 20%;
line-height: 45px;
color: #aaa;
background: #f5f5f5;
margin-left: -1px;
text-align: center;
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
}
.dropdown-menu {
display: none;
position: absolute;
left: 0px;
text-align: center;
margin-top: -1px;
background-color: #85C0E5;
list-style: none;
border-bottom-right-radius: 3px;
border-bottom-left-radius: 3px;
}
.dropdown-menu li {
width: 255px;
margin-left: -4px;
line-height: 45px;
color: #fff;
border-top: 1px solid #ddd;
}
.dropdown-menu.active {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<li class="button-dropdown sort">
<p class="dropdown-toggle">Newest</p>
<ul class="dropdown-menu">
<li>Oldest</li>
<li>Most Liked</li>
<li>Most Read</li>
</ul>
</li>
我希望不使用选择框来动态更改框的内容。为什么?因为我不知道如何使用选择框来实现这个结果。我根本无法按照我想要的方式设置下拉菜单的样式。
【问题讨论】:
-
不太清楚具体的问题或疑问是什么
-
现在我只有下拉菜单,但我还需要它作为一个选择框。意思是,当我按下下拉列表中的
- 之一时,顶部
- 的内容会改变。
-
你在使用 Bootstrap 吗?如果是,他们已经拥有built-in dropdowns,您需要做的就是创建一个新类并将其应用于父类并更改 CSS。
-
$('.dropdown-menu li').click(function(){ $(this).parent().prev().text($(this).text()); })之类的东西应该改变文本 -
@charlietfl 这实际上解决了我的部分问题,我现在需要的是,它可以替换文本。像这样: 1.
- 最新的
- 最旧的
点击 2.- 最旧的
- 最新的
标签: javascript jquery css select options