随机化nth-of-type?

Laz*_*vić -1 jquery

alert(Math.floor(Math.random()*71+1));
Run Code Online (Sandbox Code Playgroud)

返回1到71之间的数字,包括它们.

alert($(".program > div:nth-of-type(5)".text())
Run Code Online (Sandbox Code Playgroud)

返回相应父级的第5个div中的文本.然而

alert($(".program > div:nth-of-type(Math.floor(Math.random()*71+1))").text());
Run Code Online (Sandbox Code Playgroud)

根本不起作用.我哪里错了?

esy*_*cat 5

也许,你想要的是:

var rnd = Math.floor(Math.random() * 71 + 1)
$(".program > div:nth-of-type(" + rnd + ")").text()
Run Code Online (Sandbox Code Playgroud)

第一个参数$()是选择器,只是一个字符串.jQuery解析它,但不评估任何不应该在里面的JS表达式.