-1
alert(Math.floor(Math.random()*71+1));

それらを含めて 1 から 71 までの数値を返します。

alert($(".program > div:nth-of-type(5)".text())

対応する親の 5 番目の div にテキストを返します。まだ

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

まったく機能しません。どこが間違っていますか?

4

1 に答える 1

5

おそらく、あなたが望むのは次のとおりです。

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

の最初のパラメーター$()はセレクターで、単なる文字列です。jQuery はそれを解析しますが、内部にあるはずのない JS 式を評価しません。

于 2013-07-04T23:18:29.507 に答える