随机函数随机字母

时间:2013-10-28 15:37:56

标签: javascript jquery html javascript-events

我是新来的,我需要帮助。我有一个随机播放功能,可以通过按class="reset" img标签上的onload=""重置按钮来调用,我需要知道如何从开始加载它。我的意思是当我在body标签上加载此页面时<div id="container" class='columns'> <div class="column" draggable="true"><header>A</header></div> <div class="column" draggable="true"><header>B</header></div> <div class="column" draggable="true"><header>C</header></div> <div class="column" draggable="true"><header>D</header></div> <div class="column" draggable="true"><header>E</header></div> <div class="column" draggable="true"><header>F</header></div> <div class="column" draggable="true"><header>G</header></div> <div class="column" draggable="true"><header>H</header></div> <div class="column" draggable="true"><header>I</header></div> <div class="column" draggable="true"><header>J</header></div> <div class="column" draggable="true"><header>K</header></div> <div class="column" draggable="true"><header>L</header></div> <div class="column" draggable="true"><header>M</header></div> <div class="column" draggable="true"><header>N</header></div> <div class="column" draggable="true"><header>O</header></div> <div class="column" draggable="true"><header>P</header></div> <div class="column" draggable="true"><header>Q</header></div> <div class="column" draggable="true"><header>R</header></div> <div class="column" draggable="true"><header>S</header></div> <div class="column" draggable="true"><header>T</header></div> <div class="column" draggable="true"><header>U</header></div> <div class="column" draggable="true"><header>V</header></div> <div class="column" draggable="true"><header>W</header></div> <div class="column" draggable="true"><header>X</header></div> <div class="column" draggable="true"><header>Y</header></div> <div class="column" draggable="true"><header>Z</header></div> </div> <div class="buttons"> <a href='#' ><img class="reset" src="images/resetBtn.png" title="Resetare" ></a> </div> ,因为当我第一次看到这个页面时,字母按字母顺序显示,我需要从头开始洗牌。< / p>

这是html代码:

 jQuery(function($){

    $('img.reset').click(function(){
        $('#container').shuffle();
    });
});


(function($){

   $.fn.shuffle = function() {
       return this.each(function(){
           var items = $(this).children().clone(true);
          return (items.length) ? $(this).html($.shuffle(items)) : this;
       });
  };

   $.shuffle = function(arr) {
        for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
        return arr;
    };

})(jQuery);

JS:

{{1}}

2 个答案:

答案 0 :(得分:1)

添加一行

$(document).ready( function() { $('#container').shuffle(); } );

答案 1 :(得分:1)

在文档准备就绪上写.shuffle

$(document.ready(function() {
    //register fn.shuffle
    $('#container').shuffle(); //shuffle on document ready
    $('img.reset').click(function(){
        $('#container').shuffle();
    });
});

或者,正如@billyonecan在评论中提到的那样,你也可以触发点击事件,所以如果你想在文件就绪上做更多的事情,你可以处理。

ALTERNATIVE:

$(document.ready(function() {
    $('img.reset').trigger( "click" ); //Handling shuffle + more stuff
    $('img.reset').click(function(){
        $('#container').shuffle();
        //Do some other stuff.
    });
});