jquery find link that has the same ID value than the CLASS value of another element
var currentId = $('#nav a').attr('id');
var currentClass = $('#circle-inner h1').attr('class');
if(currentClass == currentId) {
$('#nav a').addClass("cu开发者_运维知识库rrent");
}
i want to compare the two valuesand if they are the same do something (addClass)
that is what i'm trying to achieve with that piece of code...
i cannot give the value of the class. there are too many.
thanks.
If you element has more than one class use hasClass method
Does #circle-inner h1
only have one class?
var currentClass = $('#circle-inner h1').attr('class');
$("#nav a#" + currentClass).addClass("current");
or if the h1
has more than one class, or to reverse the check try:
$("#nav a").each(function (i) {
var currentId = $(this).attr('id');
if ($('#circle-inner h1').hasClass(currentId)) {
$(this).addClass('current');
}
});
Instead of direct equality try a RegExp test instead:
var currentId = $('#nav a').attr('id');
var currentClass = $('#circle-inner h1').attr('class');
if(new RegExp(currentClass).test(currentId)) {
$('#nav a').addClass("current");
}
精彩评论