[英]ToggleClass doesn't work properly with FontAwesome
I'm trying to change the triangle direction on click and with this code, everything works perfect, but now I want to change the first direction because the list is hidden, so, why when I change the first fa-caret-down with right and change the other triangle accordingly it doesn't work? 我试图更改单击的三角形方向,并使用此代码,一切正常,但是现在我想更改第一个方向,因为列表是隐藏的,所以,为什么当我用右键更改第一个fa-caret-down时并相应地更改另一个三角形不起作用?
collapsible.append('<h5>' + scheme.name + " " + '<em class="fa fa-caret-down"></span></h5>');
if ($('.SchemeSearchExp').length) {
$("div.schemeHead>h5").click(function () {
$(this).next('.streetname').toggle();
$(this).children("em").toggleClass('fa-caret-right');
});
}
Use Event Delegation using .on()
, when manipulation selector (ie removing and adding classes). 在操纵选择器(即删除和添加类)时,使用
.on()
事件委托 。
Change your code to 将您的代码更改为
$(document).on('click', ".fa-caret-right", function(){ $(this).next('.streetname').toggle(); $(this).toggleClass('fa-caret-down'); });
$(document).on('click', "em.fa", function () {
$(this).next('.streetname').toggle();
$(this).toggleClass('fa-caret-right fa-caret-down');
});
In place of document
you should use closest static container. 代替
document
您应该使用最近的静态容器。 A good read Direct and delegated events 良好阅读直接事件和委托事件
Additionally, You should use if
block in the event handlers 此外,您应该在事件处理程序中使用
if
块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.