[英]How can I make my icon toggle using jQuery and MaterializeCSS?
In MaterializeCSS there is a specific syntax to calling their premade icons. 在MaterializeCSS中,有一种特定的语法来调用其预制图标。 This is an example:
这是一个例子:
<i class="material-icons">expand_more</i>
The framework knows what icon you want based on the text between the i tags. 框架基于i标签之间的文本知道您想要的图标。 I am trying to create a collapsible div using jQuery and I would like for the icon to flip between an up and down arrow.
我正在尝试使用jQuery创建可折叠的div,并且我希望图标在上下箭头之间翻转。 In order to do this, I need to replace the text in between the i tags.
为此,我需要替换i标记之间的文本。
I am successfully able to change it once but not back when I collapse the div. 我可以一次成功更改它,但当我折叠div时不能更改。 Here is my code:
这是我的代码:
$('.remove-text').click(function() { $(this).closest('.card').toggleClass('collapsed'); if ($('.arrow-change').text = 'expand_more') { $('.arrow-change').text('expand_less'); } else { $('.arrow-change').text('expand_more'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="remove-text"> <i class="material-icons arrow-change">expand_more</i> </div>
Your use of =
is actually setting the value, what you actually want is ==
or ===
which is a comparison. 您对
=
使用实际上是在设置值,您真正想要的是==
或===
这是一个比较)。
The double equals doesn't enforce type match, but the triple does. 双重等于不强制类型匹配,但双重匹配则强制类型匹配。
Also, it's text()
, not just text
, because you're using jQuery. 另外,它是
text()
,而不仅仅是text
,因为您使用的是jQuery。 If you wanted to use vanilla, it would be innerText
. 如果要使用香草,
innerText
。
Try using this slideToggle() 尝试使用此slideToggle()
$( "#collapsibleDiv" ).slideToggle( "slow", function() {
// Animation complete.
}); });
Try with this: 试试这个:
$('.remove-text').click(function() {
var parent_child = $(this);
parent_child.toggleClass('collapsed');
if(parent_child.hasClass('collapsed')){
parent_child.children('i.arrow-change').text('expand_less');
}else{
parent_child.children('i.arrow-change').text('expand_more');
}
});
Example: https://jsfiddle.net/r5152vna/ 示例: https : //jsfiddle.net/r5152vna/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.