[英]Bootstrap Jquery <li> on click add icon to list
I have a unordered expandable bootsrap list. 我有一个无序的可扩展靴列表。
When a user selects one of the items on the list (for example Day) i want to show an icon on the right. 当用户选择列表中的项目之一(例如Day)时,我想在右侧显示一个图标。
currently i am using bootsrap hidden class to hide the span on the list. 目前,我正在使用bootsrap隐藏类来隐藏列表中的跨度。 When the user selects the option i want the icon to appear so that it looks selected.
当用户选择该选项时,我希望该图标出现,以使其看起来已选中。
I have made Bootply 我做了Bootply
Is there a better way to do this than having all the icons on the list as hidden? 有没有比隐藏列表中所有图标更好的方法呢? Currently my Jquery function is not working:
目前,我的Jquery函数无法正常工作:
JQUERY JQUERY
$('ul.list-group li').click(function(e)
{
$('li.list-group-item').removeClass('list-group-item');
});
CSS 的CSS
<ul class="list-group">
<li class="list-group-item">Monday <span class="glyphicon glyphicon-ok pull-right hidden"></span></li>
<li class="list-group-item">Tuesday<span class="glyphicon glyphicon-ok pull-right hidden"></span></li>
<li class="list-group-item">Wednesday<span class="glyphicon glyphicon-ok pull-right hidden"></span></li>
<li class="list-group-item">Thursday<span class="glyphicon glyphicon-ok pull-right hidden"></span></li>
<li class="list-group-item">Friday<span class="glyphicon glyphicon-ok pull-right hidden"></span></li>
<li class="list-group-item">Saturday<span class="glyphicon glyphicon-ok pull-right hidden"></span></li>
<li class="list-group-item">Sunday<span class="glyphicon glyphicon-ok pull-right hidden"></span></li>
</ul>
Something like this should work: 这样的事情应该起作用:
$('.list-group-item').click(function() {
$(this).children('span').toggleClass('hidden');
});
What this will do is toggle the hidden class when when you click a list item. 这将是您单击列表项时切换隐藏的类的方法。 However, it still requires the hidden class.
但是,它仍然需要隐藏的类。
What you could do is, in your css, something like this: 您可以在CSS中执行以下操作:
.list-group-item span {
display: none;
}
.list-group-item.active span {
display: block;
}
And then in your javascript you could do this: 然后在您的JavaScript中,您可以执行以下操作:
$('.list-group-item').click(function() {
$(this).toggleClass('active');
});
What you have to do is just toggle
the hidden
class from the span
element when clicking li.list-group-item
您要做的就是单击
li.list-group-item
时,从span
元素中toggle
hidden
类。
$('li.list-group-item').click(function(e) {
$(this).children('span').toggleClass('hidden');
});
$('li.list-group-item').click(function(e) { $(this).children('span').toggleClass('hidden'); });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="list-group"> <li class="list-group-item">Monday <span class="glyphicon glyphicon-ok pull-right hidden"></span> </li> <li class="list-group-item">Tuesday<span class="glyphicon glyphicon-ok pull-right hidden"></span> </li> <li class="list-group-item">Wednesday<span class="glyphicon glyphicon-ok pull-right hidden"></span> </li> <li class="list-group-item">Thursday<span class="glyphicon glyphicon-ok pull-right hidden"></span> </li> <li class="list-group-item">Friday<span class="glyphicon glyphicon-ok pull-right hidden"></span> </li> <li class="list-group-item">Saturday<span class="glyphicon glyphicon-ok pull-right hidden"></span> </li> <li class="list-group-item">Sunday<span class="glyphicon glyphicon-ok pull-right hidden"></span> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.