开发者

jQuery setting class to a variable

开发者 https://www.devze.com 2022-12-18 19:33 出处:网络
I am trying to set class to a variable. My code is: var vtxt = $(this).attr(\"id\"); $(\"(\'\" + vtxt + \"\')\").addClass(\"on\");

I am trying to set class to a variable. My code is:

var vtxt = $(this).attr("id");
$("('" + vtxt + "')").addClass("on");

It does not work this way. I've tried several things with no luck. What is the right format for adding a setting class to a variable开发者_如何学JAVA?

Thank you


Do this:

var vtxt = $(this).attr('id');
$("#"+vtxt).addClass('on');

But, it's worth noting that (given the above example) you can just do this and get the same result:

$(this).addClass('on');
0

精彩评论

暂无评论...
验证码 换一张
取 消