首页 > 解决方案 > 在每个 javascript 中获取多个值

问题描述

我必须在 n 个 li 的长 ul 中附加一个 li。lis 的位置需要是第 10、20、30 等等。这是我的代码。当我使用静态数字运行时,它工作正常,但是当我使用变量运行时,它不起作用。原因是数字永远不等于变量。我的代码有什么错误,有人可以帮忙吗?

require(['jquery', 'jquery/ui', 'domReady!'], function(jquery) {
  jquery(document).ready(function() {

    jquery("ol#list > li").each(function(index) {
      var getXth = '';
      var tenth = 10;
      var str = parseInt(index);
      var getTenth = parseInt(str * 10);
      var b = 8;
      var getXth = parseInt(getTenth, 10) + parseInt(b, 10);

      alert(index);
      alert('this' + getXth);

      //var getXth = parseInt(str) * tenth;
      //var getValTenth = getXth - 2;
      //alert(typeof sum);

      /*index  == 1 || index  == 8 || index  == 18 || index  == 28 || index  == 38 || index  == 48 || index  == 58 || index  == 68 || index  == 78 || index  == 88 || index  == 98 || index  == 108 || index  == 118 || index  == 128 || index  == 138 || index  == 148 || index  == 158 || index  == 168 || index  == 178 || index  == 188 || index  == 198 || index  == 208 || index  == 218 || index  == 228 || index  == 238 || index  == 248 || index  == 258 || index  == 268 || index  == 278 || index  == 288*/
      //index  == (parseInt(getTenth))

      if (index == getXth) {
        jquery('#list > li:eq(' + index + ')').after('<li class="item product product-item col-lg-4 col-md-4 col-sm-4 col-xs-6">' + jquery("div.tenth-product-block").html() + '</li>');
      }

      var getXth = '';
      //index +=1;
    });
  });
});

标签: javascriptjqueryforeacheach

解决方案


if(index%10 == 1) { 

                    jquery('#list > li:eq('+index+')').after('<li class="item product product-item col-lg-4 col-md-4 col-sm-4 col-xs-6">'+jquery("div.tenth-product-block").html()+'</li>');
                }

这是我的代码。正如我在函数之后使用的,所以它在之后添加了 html。


推荐阅读