jquery 根据class名 赋值_jQuery使用变量名称通过类选择元素

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?

i.e. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值