admin管理员组文章数量:1026989
I'm trying to make a navigation bar where the current tab you're visiting has a different background color, by adding a CSS class (the "clickedNav" class). I have a loop to iterate through and remove the color (so only the current tab is highlighted), and then I add the color to the clicked link. Right now, it's adding the class (clickedNav) successfully, but my loop to remove them beforehand isn't removing them (I'm seeing lots of "HEYS", but no "yup"s). Here's the relevant code:
$('.navlink').click(function(e){
console.log("HEY");
var self = $(this);
$('.navlink').each(function(i) {
if($('.navlink').hasClass('.clickedNav')){
console.log("yup");
$('.navlink').removeClass('clickedNav');
}
});
$(this).addClass('clickedNav');
event.stopPropagation();
});
What am I doing wrong?
EDIT: Thanks for the help folks. See scrblndr3's response for the fix to my code as posted, and Pinpickle's response for a more efficient resolution.
I'm trying to make a navigation bar where the current tab you're visiting has a different background color, by adding a CSS class (the "clickedNav" class). I have a loop to iterate through and remove the color (so only the current tab is highlighted), and then I add the color to the clicked link. Right now, it's adding the class (clickedNav) successfully, but my loop to remove them beforehand isn't removing them (I'm seeing lots of "HEYS", but no "yup"s). Here's the relevant code:
$('.navlink').click(function(e){
console.log("HEY");
var self = $(this);
$('.navlink').each(function(i) {
if($('.navlink').hasClass('.clickedNav')){
console.log("yup");
$('.navlink').removeClass('clickedNav');
}
});
$(this).addClass('clickedNav');
event.stopPropagation();
});
What am I doing wrong?
EDIT: Thanks for the help folks. See scrblndr3's response for the fix to my code as posted, and Pinpickle's response for a more efficient resolution.
Share Improve this question edited Feb 4, 2015 at 3:41 Liftoff 25.5k14 gold badges70 silver badges126 bronze badges asked Jan 10, 2014 at 3:08 BerBer 6951 gold badge11 silver badges17 bronze badges2 Answers
Reset to default 4No need for the .each function, just use the selector to your advantage (and take the "."
out of the removeClass
as scrblnrd3 said)
$('.navlink.clickedNav').removeClass('clickedNav');
This way, the function would only be performed on those elements that have the class (though you could just use '.navlink'
as the selector and it will do the exact same thing)
You have a couple of problems. Firstly, in your hasClass
, you are putting your classes with a dot in front of them. That's unnecessary. Secondly, you have defined your event has e
, but you're using it as event
. Thirdly, you should use $(this)
in your .each()
This should fix the problem
$('.navlink').click(function(e){
console.log("HEY");
var self = $(this);
$('.navlink').each(function(i) {
if($(this).hasClass('clickedNav')){ //No need for a period
console.log("yup");
$(this).removeClass('clickedNav');
}
});
$(this).addClass('clickedNav');
e.stopPropagation();
});
I'm trying to make a navigation bar where the current tab you're visiting has a different background color, by adding a CSS class (the "clickedNav" class). I have a loop to iterate through and remove the color (so only the current tab is highlighted), and then I add the color to the clicked link. Right now, it's adding the class (clickedNav) successfully, but my loop to remove them beforehand isn't removing them (I'm seeing lots of "HEYS", but no "yup"s). Here's the relevant code:
$('.navlink').click(function(e){
console.log("HEY");
var self = $(this);
$('.navlink').each(function(i) {
if($('.navlink').hasClass('.clickedNav')){
console.log("yup");
$('.navlink').removeClass('clickedNav');
}
});
$(this).addClass('clickedNav');
event.stopPropagation();
});
What am I doing wrong?
EDIT: Thanks for the help folks. See scrblndr3's response for the fix to my code as posted, and Pinpickle's response for a more efficient resolution.
I'm trying to make a navigation bar where the current tab you're visiting has a different background color, by adding a CSS class (the "clickedNav" class). I have a loop to iterate through and remove the color (so only the current tab is highlighted), and then I add the color to the clicked link. Right now, it's adding the class (clickedNav) successfully, but my loop to remove them beforehand isn't removing them (I'm seeing lots of "HEYS", but no "yup"s). Here's the relevant code:
$('.navlink').click(function(e){
console.log("HEY");
var self = $(this);
$('.navlink').each(function(i) {
if($('.navlink').hasClass('.clickedNav')){
console.log("yup");
$('.navlink').removeClass('clickedNav');
}
});
$(this).addClass('clickedNav');
event.stopPropagation();
});
What am I doing wrong?
EDIT: Thanks for the help folks. See scrblndr3's response for the fix to my code as posted, and Pinpickle's response for a more efficient resolution.
Share Improve this question edited Feb 4, 2015 at 3:41 Liftoff 25.5k14 gold badges70 silver badges126 bronze badges asked Jan 10, 2014 at 3:08 BerBer 6951 gold badge11 silver badges17 bronze badges2 Answers
Reset to default 4No need for the .each function, just use the selector to your advantage (and take the "."
out of the removeClass
as scrblnrd3 said)
$('.navlink.clickedNav').removeClass('clickedNav');
This way, the function would only be performed on those elements that have the class (though you could just use '.navlink'
as the selector and it will do the exact same thing)
You have a couple of problems. Firstly, in your hasClass
, you are putting your classes with a dot in front of them. That's unnecessary. Secondly, you have defined your event has e
, but you're using it as event
. Thirdly, you should use $(this)
in your .each()
This should fix the problem
$('.navlink').click(function(e){
console.log("HEY");
var self = $(this);
$('.navlink').each(function(i) {
if($(this).hasClass('clickedNav')){ //No need for a period
console.log("yup");
$(this).removeClass('clickedNav');
}
});
$(this).addClass('clickedNav');
e.stopPropagation();
});
本文标签: javascriptjQuery Iterating through each element with CSS classStack Overflow
版权声明:本文标题:javascript - jQuery: Iterating through each element with CSS class - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745657059a2161658.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论