我是jquery的新手。 我试图将列数据/名称作为变量从数组中传递。列分配不起作用。注释部分可以工作,但是对列字段的变量赋值不起作用。有人可以帮忙吗?以下是摘录:
<html>
<head>
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
</head>
<body>
<script>
var dataSet = [
["Tiger Nixon1", "10", "20", "51", "300"],
["Tiger Nixon2", "10", "20", "51", "300"],
["Tiger Nixon3", "10", "20", "51", "300"]
];
$(document).ready(function() {
var colname1= [
{ title: "Name" },
{ title: "column1" },
{ title: "column2" },
{ title: "column3" },
{ title: "column4" }];
var i = 2;
function myFunc1(item, index, arr) {
arr[index].title = item;
value2.innerHTML = colname1[index].title;
}
/*-----------not working--------------------*/
var t = $('#example').DataTable({
data: dataSet,
columns: colname1.forEach(myFunc1, i)
});
/*-----------working--------------------*/
/* var t = $('#example').DataTable({
data: dataSet,
columns: [
{ title: "Name" },
{ title: "column1" },
{ title: "column2" },
{ title: "column3" },
{ title: "column4" }]
});*/
});
</script>
<div > <span> this is test text</span> </div>
<div class="content"> <span id="value1"> this is test2 section</span> </div>
<div class="content"> <span id="value2"> this is test3 section</span> </div>
<div>
<table width="300" class="display" id="example"></table>
</div>
</body>
</html>
&#13;
提前感谢您的帮助。