【发布时间】:2018-11-03 19:27:42
【问题描述】:
我在这里有一个简单的概念。单击控制台中的第一个按钮时,我会得到一些值,即增量索引++。我已经将值合并并放入第二个按钮的 onclick 数组中,现在输出为
["$('#chart1')", "$('#chart2')", "$('#chart3')"],
但我唯一需要解决的是删除数组中每个值周围的双引号。我需要这样的输出
[ $('#chart1'), $('#chart2'),$('#chart3')]
下面是代码。
html
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div><button id="button1" class="button1">submit1</button> </div>
<div><button id="garray" class="button1">genarete-array</button> </div>
脚本
var index = 0;
var id = [];
$('#button1').on('click', function() {
index++;
id.push(`$(\'#chart${index}\')`)
console.log('chart' + index);
});
$('#garray').on('click', function() {
console.log(id);
//output [ $('#chart1'), $('#chart2'),$('#chart3')]
})
【问题讨论】:
-
你不能那样做。因为当你推送到数组时它会变成字符串
-
你可以尝试添加数字/承诺/对象:var arr = []; arr.push(3);控制台.log(arr[0] === '3'); // 错误