更改字体真棒图标onclick功能

时间:2017-08-03 20:52:20

标签: javascript jquery html font-awesome show-hide

当用户点击按钮时,我正试图将.fa-eye交换为fa-eye-slash。我究竟做错了什么?它不起作用。

HTML code:

<button onclick="arata_ascunde(this);" style="align:right;font-size:13px" 
class="btn btn-info " id="show_hide_bt" style="background-color:#00b0ff;"><i 
class="fa fa-eye"></i> Show</button>

Javascript代码:

 function arata_ascunde(button) {
     var x = document.getElementById('showhide');
     var change = document.getElementById("show_hide_bt");

     if (x.style.display === 'none') {
       x.style.display = 'block';
     } else {
       x.style.display = 'none';
     }

     if (change.innerHTML == ' Show')
            {

                change.innerHTML = ' Hide';
                $(button).find('i').toggleClass('fa-eye').toggleClass('fa-eye-slash');
            }
            else {

                change.innerHTML = ' Show';
                $(button).find('i').toggleClass('fa-eye-slash').toggleClass('fa-eye');
            }


  }

5 个答案:

答案 0 :(得分:2)

document.querySelector('button').addEventListener('click', function() {
  const icon = this.querySelector('i');
  const text = this.querySelector('span');

  if (icon.classList.contains('fa-eye')) {
    icon.classList.remove('fa-eye');
    icon.classList.add('fa-eye-slash');
    text.innerHTML = 'Hide';
  } else {
    icon.classList.remove('fa-eye-slash');
    icon.classList.add('fa-eye');
    text.innerHTML = 'Show';
  }
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<button><i class="fa fa-eye"></i> <span>Show</span></button>

答案 1 :(得分:1)

&#13;
&#13;
function arata_ascunde(button) {
   var x = $('#showhide');
   $(button).find('i').remove();
   if ($(button).text().trim() == 'Show') {
     $(button).html($('<i/>',{class:'fa fa-eye-slash'})).append(' Hide');
     x.fadeIn();
    }
    else {
      $(button).html($('<i/>',{class:'fa fa-eye'})).append(' Show');
      x.fadeOut();
    }
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button onclick="arata_ascunde(this);" style="align:right;font-size:13px"
            class="btn btn-info " id="show_hide_bt" style="background-color:#00b0ff;">
        <i class="fa fa-eye"></i> Show
    </button>
    <div id="showhide" style="background-color:red;width:100px;height:100px;margin:10px;display:none;"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

因为您已插入Jquery。您可以通过Jquery轻松完成此任务。

$("#show_hide_bt").click(function(event) {
    $(this).find('i').toggleClass('fa-eye-slash');
});

现在Jquery的代码是

$(this).find('i').toggleClass('fa-eye');

如果你想删除fa-eye类,那么你可以链接另一个toggleClass或再次添加它

$(this).find('i').toggleClass('fa-eye-slash').toggleClass('fa-eye');

或者以单行添加。评论这是否有帮助

In [1194]: def get_key(l, key):
      ...:     for k, v in l:
      ...:         if k == key:
      ...:             yield v
      ...:             

In [1195]: list(get_key(List1, 'minion 2015'))
Out[1195]: [1.0]

答案 3 :(得分:0)

我认为您可以通过简化HTML和JQuery来获得所需的结果。

试试这个代码段:

$(document).ready(() => {
  const button = $(".btn");

  button.click(() => {
    if (button.text() == " Show") {
      button.html('<i class="fa fa-eye-slash"></i> Hide');
    } else {
      button.html('<i class="fa fa-eye"></i> Show');
    }
  });
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button class='btn btn-default'><i class="fa fa-eye"></i> Show</button>

您可以像这样切换类:$('i').toggleClass("fa-eye-slash fa-eye");,但由于您也想更改文本,我建议您使用上述解决方案。

答案 4 :(得分:0)

function myFunction(x) {
  x.classList.toggle("fa-thumbs-down");
}
.fa {
  font-size: 50px;
  cursor: pointer;
  user-select: none;
}

.fa:hover {
  color: darkblue;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<p>Click on the icon to toggle between thumbs-up and thumbs-down (like/dislike):</p>
<i onclick="myFunction(this)" class="fa fa-thumbs-up"></i>