Javascript 嵌套的 .each() 函数

标签 javascript jquery arrays

我尝试创建一个计算器键盘。 onclick 功能正常工作。我的问题是 .each() 函数。我怎样才能遍历我的 buttonArray ?。我无法处理嵌套数组并附加 <p><div>同样<input>附加到 <p> .

我的脚本是这样的:

var buttonArray = [
  [{
      type: 'button',
      className: 'sil',
      value: 'C'
    }, {
      type: 'button',
      className: 'hepsiniSil',
      value: 'AC'
    },

  ],
  [{
    type: 'button',
    className: 'buttons',
    value: '7'
  }, {
    type: 'button',
    className: 'buttons',
    value: '8'
  }, {
    type: 'button',
    className: 'buttons',
    value: '9'
  }, {
    type: 'button',
    className: 'buttons',
    value: '*'
  }],
  [{
    type: 'button',
    className: 'buttons',
    value: '4'
  }, {
    type: 'button',
    className: 'buttons',
    value: '5'
  }, {
    type: 'button',
    className: 'buttons',
    value: '6'
  }, {
    type: 'button',
    className: 'buttons',
    value: '-'
  }],
  [{
    type: 'button',
    className: 'buttons',
    value: '1'
  }, {
    type: 'button',
    className: 'buttons',
    value: '2'
  }, {
    type: 'button',
    className: 'buttons',
    value: '3'
  }, {
    type: 'button',
    className: 'buttons',
    value: '+'
  }],
  [{
    type: 'button',
    className: 'buttons',
    value: '0'
  }, {
    type: 'button',
    className: 'esit',
    value: '=',
    click: 'esittir'
  }, {
    type: 'button',
    className: 'buttons',
    value: '/'
  }]
]

$(document).ready(function() {
  $.each(function(index, buttonArray) {
    $("<p>").each(function(subIndex, subArrays) {
      $("<input>")
        .addClass(subArrays.className)
        .val(subArrays.val)
        .appendTo(this)
    });
  });
});

我想要这个输出:

<p>
  <input type="button" class="sil" value="C" style="width:50px">
  <input type="button" class="hepsiniSil" value="AC" style="width:50px">
</p>
<p>
  <input type="button" class="buttons" value="7">
  <input type="button" class="buttons" value="8">
  <input type="button" class="buttons" value="9">
  <input type="button" class="buttons" value="*">
</p>
<p>
  <input type="button" class="buttons" value="4">
  <input type="button" class="buttons" value="5">
  <input type="button" class="buttons" value="6">
  <input type="button" class="buttons" value="-">
</p>
<p>
  <input type="button" class="buttons" value="1">
  <input type="button" class="buttons" value="2">
  <input type="button" class="buttons" value="3">
  <input type="button" class="buttons" value="+">
</p>
<p>
  <input type="button" class="buttons" value="0">
  <input type="button" class="esit" value="=" style="width:50px" onclick='esittir()'>
  <input type="button" class="buttons" value="/">

</p>

最佳答案

$.each 第一个参数是要迭代的数组,第二个是回调:

jQuery.each( array, callback )

回调返回迭代项作为第二个参数:

callback

Type: Function( Integer indexInArray, Object value ) The function that will be executed on every object.

另外this$.each()是项目的值(value),你不能附加到它。

您需要迭代外部数组,创建一个 <p> element 把他追加到容器中。然后迭代子数组,创建<input> (或只是一个 <button> 元素),并将它们附加到它们的 <p> 中容器:

var buttonArray = [
  [{
      type: 'button',
      className: 'sil',
      value: 'C'
    }, {
      type: 'button',
      className: 'hepsiniSil',
      value: 'AC'
    },

  ],
  [{
    type: 'button',
    className: 'buttons',
    value: '7'
  }, {
    type: 'button',
    className: 'buttons',
    value: '8'
  }, {
    type: 'button',
    className: 'buttons',
    value: '9'
  }, {
    type: 'button',
    className: 'buttons',
    value: '*'
  }],
  [{
    type: 'button',
    className: 'buttons',
    value: '4'
  }, {
    type: 'button',
    className: 'buttons',
    value: '5'
  }, {
    type: 'button',
    className: 'buttons',
    value: '6'
  }, {
    type: 'button',
    className: 'buttons',
    value: '-'
  }],
  [{
    type: 'button',
    className: 'buttons',
    value: '1'
  }, {
    type: 'button',
    className: 'buttons',
    value: '2'
  }, {
    type: 'button',
    className: 'buttons',
    value: '3'
  }, {
    type: 'button',
    className: 'buttons',
    value: '+'
  }],
  [{
    type: 'button',
    className: 'buttons',
    value: '0'
  }, {
    type: 'button',
    className: 'esit',
    value: '=',
    click: 'esittir'
  }, {
    type: 'button',
    className: 'buttons',
    value: '/'
  }]

];


var $calculator = $('#calculator');

$.each(buttonArray, function(index, buttons) {
  var $p = $('<p>').appendTo($calculator);

  $.each(buttons, function(subIndex, button) {
    $('<input>')
      .addClass(button.className)
      .prop('type', button.type)
      .val(button.value)
      .appendTo($p)
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="calculator"></div>

关于Javascript 嵌套的 .each() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671811/

相关文章:

javascript - 防止自定义 jQuery 图像轮播中的事件重叠

javascript - 调整 HTML 内容的大小以使其适合 div 并且还需要保持纵横比

javascript项目拼接自己超出列表

javascript - 缩小目录中的所有 CSS 和 Javascript

javascript - 调试 Angular 前端

javascript - 如何在html中使用ajax方法从 Node 服务器获取响应

c++ - 对字符串数组进行排序

javascript - 使用 p :commandButton and p:dialog 时出现 Javascript 错误

javascript - JQuery/JavaScript - 突出显示输入或文本区域中的一部分文本

java - 试图记住疯狂的java数组技巧