[英]jQuery: change link text and icon on click
So I am trying to change the the icon of my link for collapse section alongside with the text of the link.因此,我试图将折叠部分的链接图标与链接文本一起更改。
Here is my code:这是我的代码:
<a href="#collapse", class="btn", role="button", data-bs-toggle="collapse" id="btn-collapse">
<i class="material-icons" style="vertical-align: middle;">expand_more</i>
<label style="vertical-align: middle;">PRIMARY TEXT</label>
</a>
<!--CONTENT-->
<script>
$(document).ready(function(){
$('#btn-collapse').on('click', function () {
var text=$('#btn-collapse').text();
if(text === "SECONDARY TEXT"){
$(this).text('PRIMARY TEXT');
$(this).find("i").html("expand_more");
} else{
$(this).text('SECONDARY TEXT');
$(this).find("i").html("expand_less");
}
});
});
</script>
I've been trying to find a solution for this for a while now, but nothing seems to work.. Text changes fine, but the icon completely vanishes on click.一段时间以来,我一直在尝试为此寻找解决方案,但似乎没有任何效果。文本更改正常,但图标在单击时完全消失。 I don't think it is a major issue, but there is something I don't get with this.
我不认为这是一个大问题,但有一些我不明白的地方。 Any help here how to adjust?
这里有任何帮助如何调整?
$(document).ready(function() { $('#btn-collapse').on('click', function() { var thisElement = $(this); var anchorLabelElement = thisElement.find('label'); if (anchorLabelElement.text() === "SECONDARY TEXT") { anchorLabelElement.text('PRIMARY TEXT'); thisElement.find("i").text('expand_more'); } else { anchorLabelElement.text('SECONDARY TEXT'); thisElement.find("i").text("expand_less"); } }); });
<link href="https://cdn.jsdelivr.net/npm/material-design-icons-iconfont@6.7.0/dist/material-design-icons.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#collapse" , class="btn" , role="button" , data-bs-toggle="collapse" id="btn-collapse"> <i class="material-icons" style="vertical-align: middle;">expand_more</i> <label style="vertical-align: middle;">PRIMARY TEXT</label> </a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.