简体   繁体   English

仅更改字体真棒图标

[英]Change Font Awesome Icon Only

I am trying to change the icon from chevron-down to chevron-up when clicked.我试图在单击时将图标从 V 形向下更改为 V 形向上。 Currently, it shows "Categories" and "Hide" when clicked.当前,单击时会显示“类别”和“隐藏”。 I would like to have it say "Categories" the whole time.我想让它一直说“类别”。 Is that possible?那可能吗?

Here is the code:这是代码:

HTML HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<h1 onclick="arata_ascunde(this);" class="btn btn-info " id="show_hide_bt">
    <i class="fa fa-chevron-down"></i> Categories
</h1>

<div id="showhide" style="display:none;">
        <ul>
            <li><a href="#">Hello World</a>
            </li>
        </ul>
</div>

JS JS

function arata_ascunde(h1) {
    var x = $('#showhide');
    $(h1).find('i').remove();
    if ($(h1).text().trim() == 'Categories') {
        $(h1).html($('<i/>',{class:'fa fa-chevron-up'})).prepend('Hide ');
        x.fadeIn();
    }
    else {
        $(h1).html($('<i/>',{class:'fa fa-chevron-down'})).prepend('Categories ');
        x.fadeOut();
    }
}

CodePen: https://codepen.io/chadwicked123/pen/porRoOq代码笔: https ://codepen.io/chadwicked123/pen/porRoOq

Your "if condition" is on something that can't be detected if it will be the same in all conditions.如果在所有条件下都相同,则您的“如果条件”是无法检测到的。 So it's better to change your condition to something more specific, like the tag's class.因此,最好将您的条件更改为更具体的内容,例如标签的类。 like this :像这样 :

function arata_ascunde(h1) {
    var x = $('#showhide');
    if ($(h1).find('i').hasClass('fa-chevron-down')) {
        $(h1).find('i').remove();
        $(h1).html($('<i/>',{class:'fa fa-chevron-up'})).append(' Categories');
        x.fadeIn();
    } else {
        $(h1).find('i').remove();
        $(h1).html($('<i/>',{class:'fa fa-chevron-down'})).append(' Categories');
        x.fadeOut();
    }
}

I changed a few things to make your result more beautiful.我改变了一些东西,让你的结果更漂亮。 This code still can be improved (I'm not a front-end developer).这段代码仍然可以改进(我不是前端开发人员)。

I used basic javascript and some logic to change the HTML based on a class我使用基本的 javascript 和一些逻辑来根据类更改 HTML

Here's the codepen for it - Link这是它的代码笔 -链接

Here's the edited JS file :这是编辑后的 ​​JS 文件:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<h1 onclick="arata_ascunde(this);" class="btn btn-info " id="show_hide_bt">
     Categories<i class="fa fa-chevron-down"></i>
</h1>

<div id="showhide" style="display:none;">
        <ul>
            <li><a href="#">Hello World</a>
            </li>
        </ul>
</div>

and the edited JS file和编辑过的 JS 文件

function arata_ascunde(h1) {
    var x = $('#showhide');
    $(h1).find('i').remove();
    if (document.querySelector("h1").classList.contains("down")) {
        $(h1).html($('<i/>',{class:'fa fa-chevron-up'})).prepend('Categories ');
      document.querySelector("h1").classList.remove("down")
        x.fadeIn();
    }
    else {
        $(h1).html($('<i/>',{class:'fa fa-chevron-down'})).prepend('Categories ');
      document.querySelector("h1").classList.add("down")
        x.fadeOut();
    }
}

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

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