how to add a class numbered from 4 onwards
I have added classes to all of my elements but I want the classes to start from the number 4(first class is .cc_content_4 and the next .cc_content_5 and so on) so I tried t开发者_运维问答o change the number the counter is set to. It didn't work Any ideas? Here's my code
$(".cc_submenu").find("ul.portfolio-list li").each(function(i){
i == 4;
$(this).addClass("cc_content_"+i);
var href = $(this).find("a").attr("href");
$(this).find("a").attr("href","#");
$('.cc_content .cc_content_'+i).load(href);
});
You're using i == 4
which is an equality operator, not an assignment. Just change it to i += 4
;
$(".cc_submenu").find("ul.portfolio-list li").each(function(i){
//i == 4; // This part will just return true/false, not change i
i += 4; // this is what you're looking for.
$(this).addClass("cc_content_"+i);
var href = $(this).find("a").attr("href");
$(this).find("a").attr("href","#");
$('.cc_content .cc_content_'+i).load(href);
});
First and foremost, you are using the equality operator instead of assignment (==
instead of =
). Second, you could setup a variable outside the each
iteration function and then increment the variable by one:
var i = 4;
$(".cc_submenu").find("ul.portfolio-list li").each(function(){
$(this).addClass("cc_content_"+i);
var href = $(this).find("a").attr("href");
$(this).find("a").attr("href","#");
$('.cc_content .cc_content_'+i).load(href);
i += 1;
});
As a note:
i == 4;
- this expression reports either true or false, but the return value is not stored anywhere. This isn't needed for your code.
i = 4;
- this expression sets i
to 4
.
$(".cc_submenu").find("ul.portfolio-list li:gt(3)").each(function(i,value){
$(this).addClass("cc_content_"+i);
var href = $(this).find("a").attr("href");
$(this).find("a").attr("href","#");
$('.cc_content .cc_content_'+i).load(href);
});
精彩评论