【问题标题】:How to create an array from .each loop with jQuery如何使用 jQuery 从 .each 循环创建数组
【发布时间】:2011-12-06 21:07:16
【问题描述】:

如何从“.each 循环”内部创建一个数组并在循环外部使用它?

我的.each loop

       // Loop through all but button with class .apply
        $('.profile-nav ul li a').not('.apply').each( function() {

            // if currently loop through element has .cur class
            if( $(this).hasClass('cur') ) {


                //Get the first class of the match element                  
                var ClassesToApply = $(this).prop('class').split(' ')[0];

            }   
            //How can I create an array from all ClassesToApply?


            //var arr = jQuery.makeArray(ClassesToApply);
            // This will create an array, but with one element only

        });

如何从所有var = ClassesToApply 创建一个数组?

我该如何处理这个数组呢? 例如

$( allClasses from an array as a selectors).doStuff();

【问题讨论】:

  • 你会得到一个字符串数组。你想用这样的数组做什么? jQuery 方法在包含 DOM 元素的数组上执行,而不是字符串。
  • 我希望使用字符串作为选择器来显示/隐藏在不同 div 中具有相同类的元素。过滤 - 有点。

标签: javascript jquery


【解决方案1】:

如果您在each 之外声明一个变量,则它可以在each 内部访问:

var yourArray = [];
$('.profile-nav ul li a').not('.apply').each(function() {
    if($(this).hasClass('cur')) {
        yourArray.push($(this).prop('class').split(' ')[0]);
    }
});
//Here, yourArray will contain the strings you require.

尽管正如其他人所展示的,有一些方法可以显着缩短您的代码。

【讨论】:

    【解决方案2】:
    fxnReqValidation = function () {
            var InputTagArray = new Array;
            InputTagArray = document.getElementsByTagName("input");
            for (var iCnt = 1; iCnt <= InputTagArray.length; iCnt++) {
                if ((g_Json[InputTagArray[iCnt].name].required == true) && (InputTagArray[iCnt].value == "")) {
                    $("#errormsg").text("please enter all required fields");
                }
                return false;
            }
        }
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      var arr = $( 'a.cur:not(.apply)', '.profile-nav' ).map( function () {
          return $( this ).prop( 'class' ).split( ' ' )[0];
      }).get();
      

      【讨论】:

      • 很好的例子!我以前从未使用过“.map”,我需要研究一下。
      • 如果 OP 想要一个数组,不要忘记在 .map() 之后链接 .get().toArray()
      【解决方案4】:
      var list = $(".profile-nav ul li a.cur:not(.apply)");
      
      list.each(function(){
         // do your thing!
      });
      

      【讨论】:

        【解决方案5】:
        var arraySelectors = $('.profile-nav ul li a.cur:not(.apply)')
                                .toArray()
                                .map(e => '.' + Array.from(e.classList).join('.'));
        

        这个 sn-p 可能不是最优雅的,但它试图适应 OP 描述的目标。

        我不喜欢拆分类名,因为你永远不知道有多少个连续的空格。

        从 jQuery 数组中退出并进入原生数组似乎是最好的解决方案。

        • Array.from() 是 2015 年左右的 ES6
        • jQuery.toArray() 出现在 jQuery 1.4 中

        【讨论】:

          猜你喜欢
          • 2012-05-01
          • 2018-07-05
          • 1970-01-01
          • 2021-09-04
          • 2023-03-18
          • 2013-02-28
          • 2014-01-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多