jQuery - 变量无法识别,但完全相同值的手动输入是?

RGB*_*GBK 1 javascript string variables jquery

最佳解释代码:

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

√工作

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

X不起作用

这显然是jQuery编程101 ...但严重的是,为什么地球上不会那样有效?!我传递了同样的东西!

我试过它> myVar = 3n(没有字符串),显然不应该工作,但事实并非如此.

mcg*_*ilm 5

你必须连接var

 var myVar = "3n";

 $(".myParent .myChild:nth-child("+myVar+")").css('border-top-color','#ffffff');
Run Code Online (Sandbox Code Playgroud)