【问题标题】:How to get all HTML elements with a certain class attribute?如何获取具有某个类属性的所有 HTML 元素?
【发布时间】:2011-10-11 19:06:13
【问题描述】:

我需要获取所有使用某个类的元素并随机选择一个。

如何在 jQuery 中做到这一点?

【问题讨论】:

    标签: javascript jquery html random


    【解决方案1】:
    var random_elem = $('.yourclass').eq(Math.floor(Math.random()*$('.yourclass').length));
    

    【讨论】:

      【解决方案2】:

      试试这个:

      jQuery.jQueryRandom = 0;
      jQuery.extend(jQuery.expr[":"],
      {
          random: function(a, i, m, r) {
              if (i == 0) {
                  jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
              };
              return i == jQuery.jQueryRandom;
          }
      });
      
      var randomElem = $(".className:random");
      

      原文:http://blog.mastykarz.nl/jquery-random-filter/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-17
        • 2022-01-17
        • 2014-06-09
        • 2018-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多