开发者

jquery nth-child issue

开发者 https://www.devze.com 2023-02-09 08:43 出处:网络
My code is 开发者_如何学Golike this: var childNo = 2; $(\'#someSelector tr:nth-child(childNo) td:first span\').addClass(\'font-strike\');

My code is 开发者_如何学Golike this:

var childNo = 2;
$('#someSelector tr:nth-child(childNo) td:first span').addClass('font-strike');

but it doesn't add the class. If i replace childNo with 2 it works fine. any idea how i can get it to work?


you're putting the string literal 'childNo' into your selector. replace it with:

$('#someSelector tr:nth-child('+childNo+') td:first span').addClass('font-strike');

and it should work


you need to wrap your variable like :

$('#someSelector tr:nth-child('+childNo'+) td:first span').addClass('font-strike');


$('#someSelector tr:nth-child(' + childNo + ' ) td:first span').addClass('font-strike');

0

精彩评论

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

关注公众号