简体   繁体   English

单击更改Bootstrap Glyphicon

[英]Changing Bootstrap Glyphicon on click

I'm wanting to swap the second class of Bootstraps 'glyphicon' span, but instead of toggling the class, It's adding it behind, thus not changing the class at all. 我想交换Bootstraps“ glyphicon”跨度的第二类,但不是在切换该类,而是在后面添加了它,因此根本没有更改该类。

I'm new(ish) to jQuery / Javascript and I just can't get my head around this. 我是jQuery / Javascript的新手,但我对此一无所知。

Heres the 继承人

<nav class="navbar navbar-top" style="position:fixed; width:100%;">
<a class="navbar-brand" href="#">Project name</a>
<a class="navbar-brand" href="#" style="float:right;">
    <span class="glyphicon glyphicon-tasks" id="whiter"></span>
</a>

And the script is below: 脚本如下:

$('.glyphicon').click(function(){
$(this).toggleClass('glyphicon-chevron-up');

I get all the classes instead of just glyphicon-chevron-up, Im getting: 我得到了所有的类,而不仅仅是glyphicon-chevron-up,我得到了:

<span class="glyphicon glyphicon-tasks glyphicon-chevron-up"></span>

Removing the glyphicon-tasks class on Element inspect displays the Chevron, so some how it is being blocked and the tasks glyph isnt being swapped. 删除Element inspect上的glyphicon-tasks类会显示Chevron,因此它会被阻止,某些字形的任务也不会被交换。

I think you want to swap glyphicon-tasks and glyphicon-chevron-up . 我认为您想交换glyphicon-tasksglyphicon-chevron-up You need to toggle both class like following. 您需要像下面这样切换两个class

$(this).toggleClass('glyphicon-tasks glyphicon-chevron-up');

This is because your function is set to class, which mean all elements with the given class. 这是因为您的函数设置为class,这意味着具有给定类的所有元素。

To focus a specific element, provide, for example, an unique ID. 要关注特定元素,例如,提供唯一的ID。 Here, you already got one. 在这里,您已经有一个。

$('#whiter').click(function() {
    $(this).toggleClass('glyphicon-chevron-up');
});

I guess this can help 我想这可以帮助

$('.glyphicon').click(function(){
$(this).removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-up');
}

If you want to have a bit more control, use jQuery to its fullest, apply a data variable to multiple glyphicons (chances are that you'll be checkboxes, folder icons, tree icons): 如果您想拥有更多控制权,请充分利用jQuery,将data变量应用于多个字形(可能是复选框,文件夹图标,树形图标):

<nav class="navbar navbar-top" style="position:fixed; width:100%;">
<a class="navbar-brand" href="#">Project name</a>
<a class="navbar-brand" href="#" style="float:right;">
  <span><i class="glyphicon glyphicon-tasks" data-tasks="firstCollection" data-mycolor="white" data-icontype="taskIcon"></i></span>
</a>

...plus, elsewhere in your page, another glyphicon, for example (this will not be used, affect or be affected by our code): ...另外,在您页面的其他位置,例如,另一个glyphicon(它将不会被我们的代码使用,影响或受其影响):

<i class="glyphicon glyphicon-unchecked" id="checkbox_Analytics" data-foldername="group_Analytics" data-icontype="groupCheckbox"></i>

...while, on the other hand, this will be affected by our code (because of foldername match): ...同时,另一方面,这将受到我们的代码的影响(由于文件夹名称匹配):

<i class="glyphicon glyphicon-check" data-foldername="2014"  data-icontype="childCheckbox"></i>

...and in JS, toggle their values without affecting each and every other glyphicon: ...并在JS中切换它们的值,而不影响其他所有glyphicon:

$('i[data-icontype="taskIcon"]').on('click', function() {
     $('i[data-tasks="firstCollection"]').toggleClass('glyphicon-tasks glyphicon-chevron-up');
     console.log("current state now displays CHEVRON UP (true/false)? ["+$(this).hasClass('glyphicon-chevron-up')+"]");
});

...
$('i[data-icontype="childCheckbox"]').on('click', function() {
     $('i[data-foldername="2014"]').toggleClass('glyphicon-check glyphicon-unchecked');
     // Notice that you can also access the `data-foldername` variable directly for each element which has it
     var layerFolderName = $(this).closest('i').data('foldername');
     console.log("Changed glyphicon chevron in: "+layerFolderName);
});

NOTE1: one style of using glyphicons, places them inside <i> tags and references them directly thusly. 注意1:一种使用字形图标的样式,将其放置在<i>标记内,从而直接对其进行引用。

NOTE2: "white" is not, in general, a good idea for an id . 注意2:通常,对于id"white"并不是一个好主意。 I recommend another data variable, data-mycolor , which might in turn be germane to your code's logic. 我建议另一个data变量data-mycolor ,它可能反过来与您代码的逻辑密切相关。 In this example, it is set, but not really used. 在此示例中,它已设置,但并未真正使用。

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

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