如何在javascript中的数组中的特定位置插入div

时间:2017-05-25 11:52:04

标签: javascript html

我有一个数组,当我正在恢复我的阵列时,我得到的是这样的东西。

arr[0] = <li data="Dummy1" class= "Dummy1"></li>
arr[1] = <li data="Dummy2" class= "Dummy2"></li>
arr[2] = <li data="Dummy3" class= "Dummy3"></li>

现在在arr [0]之后我想插入一个带光标样式的div

<div style: 'cursor:auto'></div>

所以我的数组看起来像

arr[0] = <li data="Dummy1" class= "Dummy1"></li>
arr[1] = <div style: 'cursor:auto'></div>
arr[2] = <li data="Dummy2" class= "Dummy2"></li>
arr[3] = <li data="Dummy3" class= "Dummy3"></li>

任何人都可以帮助我如何实现这个'

2 个答案:

答案 0 :(得分:3)

你可以这样使用数组对象上的$scope.dtInstance = {}; $scope.dtOptions = DTOptionsBuilder.newOptions() ////.withOption('serverSide', true) //.withOption('hasBootstrap', true) //.withOption('processing', true) //.withDisplayLength(10) //.withPaginationType('full_numbers') .withOption('stateSave', true) //.withOption('searchDelay', 350) //.withOption('width', '100%') //.withDataProp('data'); .withOption('order', [ [0, 'desc'] ]) .withOption('bFilter', false) .withOption('lengthChange', false) .withOption('rowCallback', rowCallback) .withOption('createdRow', createdRow); function createdRow(row, data, dataIndex) { // Recompiling so we can bind Angular directive debugger; $compile(angular.element(row).contents())($scope); } function rowCallback(tabRow, data, dataIndex) { $(tabRow).unbind('click'); $(tabRow).on('click', function() { console.log('click' + data); $(this).find('.a1-icon').toggleClass('fa-rotate-180'); var tr = $(tabRow); var table = $scope.dtInstance.DataTable; var row = table.row(tr); if (row.child.isShown()) { // This row is already open - close it row.child.hide(); tr.removeClass('shown'); } else { // Open this row row.child(format(row.data())).show(); tr.addClass('shown'); } }); } function format(d) { debugger; console.log(JSON.stringify(d)); // `d` is the original data object for the row return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' + '<tr>' + '<td>Full name:</td>' + '<td>' + d.Date_of_Payment + '</td>' + '</tr>' + '<tr>' + '<td>Extension number:</td>' + '<td>' + d.Date_of_Payment + '</td>' + '</tr>' + '<tr>' + '<td>Extra info:</td>' + '<td>And any further details here (images etc)...</td>' + '</tr>' + '</table>'; }函数:

splice

这将在索引1中插入项目,删除0项。

答案 1 :(得分:1)

使用Array.splice()方法:它会通过删除现有元素和/或添加新元素来更改数组的内容。

var arr = [];

arr[0] = '<li data="Dummy1" class= "Dummy1"></li>';
arr[1] = '<li data="Dummy2" class= "Dummy2"></li>';
arr[2] = '<li data="Dummy3" class= "Dummy3"></li>';

arr.splice(1, 0, "<div style: 'cursor:auto'></div>");

console.log(arr);