[英]Jquery toggleclass won't work
My toggleclass
function won't work, and I have no clue why. 我的
toggleclass
函数不起作用,我不知道为什么。 Any help is appreciated thanks! 任何帮助表示赞赏谢谢!
jsfiddle: http://jsfiddle.net/t763P/1460/ jsfiddle: http : //jsfiddle.net/t763P/1460/
<li id="toggle-favicon">
<a href="#">Orders <span class="fas fa-chevron-left"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="#">File orders</a>
</li>
<li>
<a href="#">Product orders</a>
</li>
</ul>
</li>
$('#toggle-favicon').click(function(){
$(this).find('span').toggleClass('fa-chevron-left fa-chevron-down');
console.log($(this).find('span'));
});
In latest version of font awesome, <span class="fas fa-chevron-left"></span>
will get converted to some SVG Element. 在最新版本的字体中,
<span class="fas fa-chevron-left"></span>
将转换为某些SVG元素。 And the corresponding span element is removed. 并删除相应的span元素。
Hence, to change the class of the icon, you will need to toggle the class of this svg element. 因此,要更改图标的类,您需要切换此svg元素的类。
Also keep in mind that .toggleClass
wont work on svg
elements as jQuery v1.x
can not do that. 还要记住
.toggleClass
在svg
元素上工作,因为jQuery v1.x
不能这样做。 So you will need to use .attr("class" ... )
method. 所以你需要使用
.attr("class" ... )
方法。
$('#toggle-favicon').click(function() {
var $el = $(this).find('svg');
$el.attr("class", function() {
return $(this).attr("class").indexOf("fa-chevron-left") != -1 ? "fa-chevron-right" : "fa-chevron-left";
})
});
If you use jQuery 3.x
then you can use .toggleClass
as well... 如果你使用
jQuery 3.x
那么你也可以使用.toggleClass
......
$('#toggle-favicon').click(function() {
var $el = $(this).find('svg');
$el.toggleClass("fa-chevron-left fa-chevron-right");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.