我在li
ul
元素中使用Font Awesome Icons
问题
当用户点击用户图标时,我将图标的颜色变为黑色。如果用户点击另一个图标,它也将是黄色
如何删除已存在的黄色图标。这意味着应该只有一个黄色图标。 Fiddle Here
HTML
<ul id="user-list">
<li class='black user'><i class="fa fa-user"></i></li>
<li class='black user'><i class="fa fa-user"></i></li>
<li class='black user'><i class="fa fa-user"></i></li>
<li class='black user'><i class="fa fa-user"></i></li>
</ul>
的jQuery
$(".user").click(function (e) {
if ($(this).hasClass("yellow")) {
$(this).removeClass("yellow");
$(this).addClass("black");
} else {
$(this).removeClass("black");
$(this).addClass("yellow");
}
});
答案 0 :(得分:3)
在从元素中添加删除类之前使用$(".user.yellow").not(this).removeClass()
:
$(".user.yellow").not(this).removeClass();
您也可以使用.toggleClass()
进行检查和显示/隐藏来缩小代码范围。
var user = $(".user").click(function (e) {
user.filter('.yellow').not(this).toggleClass('yellow black');
$(this).toggleClass("yellow black");
});
<强> Working Demo 强>
答案 1 :(得分:0)
我自己的建议如下:
// binding the anonymous function of the click() method
// as the click event-handler:
$(".user").click(function (e) {
// toggling between the named classes (if the element currently
// has the class of 'black' it will be switched to 'yellow', and
// vice-versa:
$(this).toggleClass('black yellow')
// switching to the siblings, selecting only those with the
// class of 'yellow' and toggling those elements' class
// to switch from 'yellow' class-name to 'black':
.siblings('.yellow').toggleClass('yellow black');
});
$(".user").click(function(e) {
$(this).toggleClass('black yellow').siblings('.yellow').toggleClass('yellow black');
});
@import url(http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css);
#user-list {
list-style: none;
cursor: pointer;
}
li {
margin: 0 0 0.5em 0;
border: 1px solid #000;
border-radius: 1em;
padding: 0.5em;
}
.yellow {
color: yellow;
}
.black {
color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="user-list">
<li class='black user'><i class="fa fa-user"></i>
</li>
<li class='black user'><i class="fa fa-user"></i>
</li>
<li class='black user'><i class="fa fa-user"></i>
</li>
<li class='black user'><i class="fa fa-user"></i>
</li>
</ul>
此外,要使用纯JavaScript,以下也是一个选项:
function highlightOnlyActive() {
// the settings for the function:
var s = {
'activeClassName' : 'yellow',
'inactiveClassName' : 'black'
},
// caching the 'this' variable for
// use within other scopes:
current = this;
// Using Function.prototype.call() to allow the use of
// Array.prototype.forEach() on the Array-like NodeList
// returned by document.querySelectorAll(), allowing us
// to iterate over the returned NodeList as an Array and
// perform a function/action on each item returned:
Array.prototype.forEach.call(this.parentNode.children, function (el) {
// if the current Node ('el') is not the clicked-node
// ('current'):
if (el !== current) {
// we unconditionally remove the 's.activeClassName'
// and add the 's.inactiveClassName':
el.classList.remove(s.activeClassName);
el.classList.add(s.inactiveClassName);
}
});
// here we simply toggle the 's.inactiveClassName' and
// 's.activeClassName' classes:
current.classList.toggle(s.activeClassName);
current.classList.toggle(s.inactiveClassName);
}
Array.prototype.forEach.call(document.querySelectorAll('#user-list li.user'), function (elem) {
elem.addEventListener('click', highlightOnlyActive);
});
function highlightOnlyActive() {
var s = {
'activeClassName': 'yellow',
'inactiveClassName': 'black'
},
current = this;
Array.prototype.forEach.call(this.parentNode.children, function(el) {
if (el !== current) {
el.classList.remove(s.activeClassName);
el.classList.add(s.inactiveClassName);
}
});
current.classList.toggle(s.activeClassName);
current.classList.toggle(s.inactiveClassName);
}
Array.prototype.forEach.call(document.querySelectorAll('#user-list li.user'), function(elem) {
elem.addEventListener('click', highlightOnlyActive);
});
@import url(http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css);
#user-list {
list-style: none;
cursor: pointer;
}
li {
margin: 0 0 0.5em 0;
border: 1px solid #000;
border-radius: 1em;
padding: 0.5em;
}
.yellow {
color: yellow;
}
.black {
color: black;
}
<ul id="user-list">
<li class='black user'><i class="fa fa-user"></i>
</li>
<li class='black user'><i class="fa fa-user"></i>
</li>
<li class='black user'><i class="fa fa-user"></i>
</li>
<li class='black user'><i class="fa fa-user"></i>
</li>
</ul>
参考文献: