简体   繁体   English

Bootstrap-带有jQuery的图标切换

[英]Bootstrap - Icon toggle with jquery

I got a little problem trying to toggle an icon of Bootstrap. 我在尝试切换Bootstrap图标时遇到了一个小问题。 When i run code it does what expected the first time you click on the icon it toggle's, but when i click again it doesn't change. 当我运行代码时,它执行的功能与您首次单击该按钮时所期望的一样,但是当我再次单击时,它并没有改变。 Here its my code and any help will be appreciated! 在这里,我的代码和任何帮助将不胜感激!

<a><i class="icon-plus"></i></a>

<script>
  $(".icon-minus").click(function(){
    $(this).removeClass("icon-minus").addClass("icon-plus");
  });
  $(".icon-plus").click(function(){
    $(this).removeClass("icon-plus").addClass("icon-minus");
  });
</script>

Update 1: 更新1:

This icon is for a collapsible menu and the code of that can be found here :) 该图标用于可折叠菜单,其代码可在此处找到:)

jsBin demo jsBin演示

$(".icon-minus, .icon-plus").click(function(){
    $(this).toggleClass("icon-minus icon-plus");
});

Or this if you dynamically create your elements: 如果您动态创建元素,则使用以下命令:

$("#parent_el_NOT_dyn_gen").on('click','.icon-minus, .icon-plus',function(){
    $(this).toggleClass("icon-minus icon-plus");
});

The jQuery's selector selects DOM elements then applys the click handler to them. jQuery的选择器选择DOM元素,然后将click处理程序应用于它们。 It's not re-evaluating the selector after you change the classes on the element. 更改元素上的类后,它不会重新评估选择器。

You probably want the delegate() / on() method from jQuery to dynamically change the the handler that's fired when the item is clicked. 您可能希望jQuery的delegate() / on()方法动态更改单击该项目时触发的处理程序。 Delegate works with event bubbling and will handle the click and evaluate if the source of the click matches the selector (at the time of the click) as opposed to the .click() which attaches the handler directly, once (at the time of page-load or whenever the code was ran). Delegate与事件冒泡一起使用,并且将处理单击并评估单击的源是否与选择器匹配(在单击时),而不是与直接附加处理程序的.click() (在页面显示时)相匹配。 -load或运行代码的任何时候)。

Another solution is to change the handler somehow, either by evaluating what class is on the existing element or using toggleClass() which will check for a class then invert it. 另一个解决方案是以某种方式更改处理程序,方法是评估现有元素上的类,或者使用toggleClass()来检查一个类,然后将其反转。

$(".icon-minus, .icon-plus").click(function() {
   var $this = $(this);
   if ($this.hasClass("icon-plus")) {
      $this.removeClass("icon-plus").addClass("icon-minus");
      return;
   }
   if ($this.hasClass("icon-minus")) {
      $this.removeClass("icon-minus").addClass("icon-plus");
      return;
   }
});

This method will be slightly faster than using on() / delegate() because it's handled at the root handler and not bubbled & checked afterwards. 此方法将比使用on() / delegate()快一点on()因为它是在根处理程序处处理的,之后不会冒泡并不会进行检查。 It's also not susceptible to any breaks in the event bubbling. 事件冒泡时也不中断。 (ie. event.stopPropagation() ) (即event.stopPropagation()

Simple solution worked for Bootstrap 3. 简单的解决方案适用于Bootstrap 3。

$('[data-toggle="collapse"]').click(function(e) {
    $(e.target).find('.icon-minus-sign, .icon-plus-sign').toggleClass("icon-minus-sign icon-plus-sign");
});

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

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