开发者

jQuery — Variable not recognised but manual input of exact same value is?

开发者 https://www.devze.com 2023-02-20 01:39 出处:网络
Best explained with code: $(\".myParent .myChild:nth-child(3n)\").css(\'border-top-color\',\'#ffffff\');

Best explained with code:

$(".myParent .myChild:nth-child(3n)").css('border-top-color','#ffffff');

√ Works

myVar = "3n";
$(".myParent .myChild:nth-child(myVar)").css('border-top-color','#ffffff');

X Doesn't work

T开发者_JS百科his is obviously jQuery programming 101... but seriously, why on earth won't that work?! I'm passing on the same thing!

I tried it as > myVar = 3n (no string), obviously that shouldn't work, and it didn't.


you var has to be concatenated

 var myVar = "3n";

 $(".myParent .myChild:nth-child("+myVar+")").css('border-top-color','#ffffff');
0

精彩评论

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