开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜