Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图使用jQuery使div看起来随机,效果不令人满意_Javascript_Jquery - Fatal编程技术网

Javascript 试图使用jQuery使div看起来随机,效果不令人满意

Javascript 试图使用jQuery使div看起来随机,效果不令人满意,javascript,jquery,Javascript,Jquery,我试图使div在每次加载页面时以随机顺序出现 我实现了以下代码: <script type="text/javascript"> $(function() { $(window).load(function(){ $("div.container").randomize("div.random"); }); }); (function($) { $.fn.randomize = function(childElem) { return this.each(func

我试图使div在每次加载页面时以随机顺序出现

我实现了以下代码:

<script type="text/javascript">
$(function() {
  $(window).load(function(){
    $("div.container").randomize("div.random");
  });
});
(function($) {
$.fn.randomize = function(childElem) {
  return this.each(function() {
      var $this = $(this);
      var elems = $this.children(childElem);
      elems.sort(function() { return (Math.round(Math.random())-0.8); });  
      $this.remove(childElem);  
      for(var i=0; i < elems.length; i++)
        $this.append(elems[i]);      
  });    
}
})(jQuery);
</script>

<div class="container">
  <div class="random"> 1 </div>
  <div class="random"> 2 </div>
  <div class="random"> 3 </div>
  <div class="random"> 4 </div>
  <div class="random"> 5 </div>
  <div class="random"> 6 </div>
  <div class="random"> 7 </div>
  <div class="random"> 8 </div>
</div>

$(函数(){
$(窗口)。加载(函数(){
$(“div.container”)。随机化(“div.random”);
});
});
(函数($){
$.fn.randomize=函数(childElem){
返回此值。每个(函数(){
var$this=$(this);
var elems=$this.children(childElem);
sort(函数(){return(Math.round(Math.random())-0.8);});
$this.remove(childElem);
对于(变量i=0;i
每次加载页面时,div都会更改其位置/顺序。然而,从统计上看,“1”几乎总是出现在“前四名”中,相反,“8”几乎总是在列表的底部

这似乎不是真的随机的。。。我将非常感谢任何有关这方面的建议。每个部门都有同样的机会登上榜首,这一点非常重要

试试这个-

$.fn.randomize = function(selector){
    var $elems = selector ? $(this).find(selector) : $(this).children(),
        $parents = $elems.parent();

    $parents.each(function(){
        $(this).children(selector).sort(function(){
            return Math.round(Math.random()) - 0.5;
        }).remove().appendTo(this);
    });

    return this;
};

$('.container').randomize('div');

我用过费舍尔·耶茨(克努斯)的洗牌

维基百科页面:

见原SO帖子:

请参阅原始GitHub页面:

请参见此处的测试小提琴:

试试这个

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

var randomize = function (element) {
    var elems = shuffle($(element));
    $(".container").html('');
    for (var i = 0; i < elems.length; i++)
    $(".container").append(elems[i]);

}
randomize("div.random");
});
$(函数(){
函数shuffle(o){
对于(var j,x,i=o.length;i;j=Math.floor(Math.random()*i),x=o[--i],o[i]=o[j],o[j]=x);
返回o;
};
变量随机化=函数(元素){
var elems=shuffle($(元素));
$(“.container”).html(“”);
对于(变量i=0;i

工作示例在

中,它在JSFIDLE中根本不更新。是的,它不。。。试图让它在那里工作,但我无法。。。它在html中工作得很好。。。注:我是jquery初学者,抱歉:(同样的事情。我刚刚刷新了页面20次,而“1”只在“位置4”下方出现了1次……奇怪……似乎html中的顺序在“随机”和无序化之后真的有很大的不同……只是出于好奇,我尝试了不同的浏览器(Firefox和chrome)我在随机性方面看到了一些不同,或者可能是我在幻想中。这确实很有趣。也许有一天,当时间不那么紧迫的时候,我会研究这个问题。多亏了法比奥,我已经有了一个解决方案。谢谢你的努力。tho!!@Variant FYI,我也没有否决你,但是使用了“random-0.5”不建议使用。请参阅。干杯。@FábioSilva-感谢您的链接,它提供了非常丰富的信息。现在,这就是我所说的随机:D工作起来很有魅力!非常感谢!!我与其他脚本有冲突,我猜它是“$.noConflict();”这是这里的一个问题…你知道如何更改代码使其工作吗?@Akh,如果你仔细看,这是我发布的同一个算法。不同的是,我花了更多的时间试图阅读它,使用那些单字符变量和为循环排列的变量…:)@Akh nope..不是我。。。我刚把它贴出来。原始帖子和代码来自CoolAJ86。查看我帖子中的链接。干杯
$(function () {
function shuffle(o) { 
    for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

var randomize = function (element) {
    var elems = shuffle($(element));
    $(".container").html('');
    for (var i = 0; i < elems.length; i++)
    $(".container").append(elems[i]);

}
randomize("div.random");
});