简体   繁体   English

切换多个MaterializeCSS图标,而无需使用ID选择器

[英]Toggle multiple MaterializeCSS icons without using an id selector

I don't have trouble toggling an icon on one element, but if it's multiple elements I'm not sure how to do it. 我没有在一个元素上切换图标的麻烦,但是如果它是多个元素,我不确定该怎么做。 For one element I would use an id selector, but with multiple elements, it would seem really tedious to have every element include an id. 对于一个元素,我将使用id选择器,但是对于多个元素,让每个元素都包含一个id似乎真的很麻烦。

For example, this is one of my floating buttons that has a toggable icon: 例如,这是我的一个带有可切换图标的浮动按钮之一:

<a class="fixed-action-btn btn-floating btn-large" style=" id="expand2">

<i class="material-icons">unfold_more</i>

</a>

and this is the Javascript: 这是Javascript:

<script>
var button = document.getElementById("expand2");
var boolean = true;

button.onclick = function(){

if(boolean == true){

$(".btn-large").html('<i class="material-icons">unfold_less</i>');
                boolean = false;
} 

else if(boolean == false){

$(".btn-large").html('<i class="material-icons">unfold_more</i>');
                boolean = true;
}
}
</script>

I'm trying to figure out how to toggle the icon for multiple elements without the use of an id selector. 我试图弄清楚如何在不使用id选择器的情况下切换多个元素的图标。 Thanks in advance! 提前致谢!

If you are using jQuery then the simplest solution would be to attach the click event to a class. 如果您使用的是jQuery,那么最简单的解决方案是将click事件附加到类。

<script>
$('.btn-large').on('click', function() {
    var $icon = $(this).find('.material-icons');
    if($icon.text() === 'unfold_less') $icon.text('unfold_more');
    if($icon.text() === 'unfold_more') $icon.text('unfold_less');
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM