gpt4 book ai didi

Javascript 嵌套的 .each() 函数

转载 作者:行者123 更新时间:2023-11-30 07:55:32 24 4
gpt4 key购买 nike

我尝试创建一个计算器键盘。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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com