I want to do somethings in jQuery to all the elements which match on a certain class name. The class name comes from a variable. How do I do the select using by class using my variable?
var x = $(this).attr('href').slice(1);
this will set x
equal to the name of the class
I want.
now I want to select all the elements with that class name
I try stuff like this but it doesn't work
$(.x.show().addClass('active').siblings().hide().removeClass('active');
basic basically I want affect all the elements with where class = x
Finally, instead of setting x
to the class name, can I just get my array
of classes in the first variable assignment?
ie instead of
var x = $(this).attr('href').slice(1);
can I do this [pseudo code]:
var x = classname is $(this).attr('href').slice(1);
Selector is nothing more than just a string. So you could use
$('.' + classname)
where classname
is a variable that holds particular class name
尝试像这样更改你的代码
$('.' + x).show().addClass('active').siblings().hide().removeClass('active');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.