2017-03-10 14 views
0

ページが更新されるたびにこの表示をランダムな順序で行うにはどうすればよいですか。javascript - ページが更新されるたびにランダムな順序で表示

ここで私は自分のワードプレスのテキストエディタに追加したものですが、画像は完璧に表示されますが、divの順番はランダム化されません。私は何を間違えたのですか?

<br><br> 
<div class="quiz"> 
<a href="http://www.thequizmania.com/we-bet-you-cant-pass-this-tricky-general-knowledge-quiz/"> 
<img src="http://i.imgur.com/3YcBoyN.jpg"></img> 
<a> 
</div> 

<br> 
<div class="quiz"> 
<a href="http://www.thequizmania.com/what-kind-of-traveler-are-you/"> 
<img src="http://i.imgur.com/GZn9myC.jpg"></img> 
<a> 
</div> 

<br> 
<div class="quiz"> 
<a href="http://www.thequizmania.com/what-two-words-describe-you-as-a-mother/"> 
<img src="http://i.imgur.com/QnJezBF.jpg"></img> 
<a> 
</div> 

<br> 
<div class="quiz"> 
<a href="http://www.thequizmania.com/can-you-pick-the-correct-word/"> 
<img src="http://i.imgur.com/Pdi9dyo.jpg"></img> 
<a> 
</div> 

<br> 
<div class="quiz"> 
<a href="http://www.thequizmania.com/can-you-pass-this-almost-impossible-shapes-test/"> 
<img src="http://i.imgur.com/Ov5WdOg.jpg"></img> 
<a> 
</div> 

<br> 

<script> 
var cards = $(".quiz"); 
for(var i = 0; i < cards.length; i++){ 
    var target = Math.floor(Math.random() * cards.length -1) + 1; 
    var target2 = Math.floor(Math.random() * cards.length -1) +1; 
    cards.eq(target).before(cards.eq(target2)); 
} 
</script> 
+0

ところで、あなたは終了タグ '' '' ''どこでも – Optio

+0

さて、あなたに感謝しませんでしたその@Optioについてはまだ助けにならなかった。 –

+0

クラス 'quiz'を持つ要素の数を取得し、ランダムにソートされた1〜nの数の配列を作成し、このランダム化された数値の配列を参照して要素をランダムにソートすることができます。 @Mehul Mohanの答えを見てください。 –

答えて

0

あなたのコードは正常に動作します。 私はちょうど:

  1. 固定<a>タグ
  2. を追加しましたjQueryライブラリ

Here your code on Jsfiddle

+0

ありがとう@Optioそれは働いた! –

+0

@TevelZakiすべてが問題ない場合は、この回答を正しいとマークしてください。 – Optio

+0

完了!感謝の芽 –

0

Math.randomでランダム化できます。

var divContents = ['<br><div class="quiz"><a href="http://www.thequizmania.com/can-you-pass-this-almost-impossible-shapes-test/"><img src="http://i.imgur.com/Ov5WdOg.jpg"></a></div>', 'second', 'thrid', '4th', '5th', '6th']; 
 
    var len = divContents.length; 
 
    var parent = document.getElementById('parent'); 
 
    
 
    for(i=0;i<len;i++) { 
 
    var random = Math.floor(Math.random()*(divContents.length)); 
 
    parent.innerHTML += (divContents[random]); 
 
    divContents.splice(random, 1); 
 
    }
<div id="parent"> 
 

 
</div>

関連する問題