如何使用JavaScript动态地将div添加到数据表的td标记

时间:2017-10-24 06:02:48

标签: javascript jquery html css

我想将 div 添加到数据表的 td 标记中。这是我的HTML代码:

table = $("#workLocation-table").DataTable({
            data: mainArray,
            "columnDefs": [
                { className: "details-control" , "targets": [0]},
                {
                    "order": [[1, 'asc']]
                },
                {
                "targets": -1,
                "width": "10%",
                "data": null,
                "defaultContent": '<div class="edit-wrapper"><span class="icn"><i class="fa fa-pencil-square-o" aria-hidden="true" id="edit"></i><i class="fa fa-trash-o" aria-hidden="true" id="delete"></i></span></div>'
            }]
        });

我想在目标2中添加一个div,并为该div提供&#34;超长&#34; 类。

2 个答案:

答案 0 :(得分:1)

试试这个:

var target = document.getElementById("id_of_the_element_inside_which_you_want_to_insert_div");
var d = document.createElement("div"); // Create div dynamically
d.setAttribute("class","over-length"); // Add "over-length" class to the dynamically created div
target.appendChild(d); // Now insert div inside target element

答案 1 :(得分:0)

table =  $("#workLocation-table").DataTable({
        data: mainArray,
        "columnDefs": [
            { className: "details-control" , "targets": [0]},
            {
                "order": [[1, 'asc']]
            },
            {
                "targets": 2,
                render : function(data, type, row) {
                    return '<div class="over-length">'+data+'</div>'
                } 
            },
            {
                "targets": -1,
                "width": "10%",
                "data": null,
                "defaultContent": '<div class="edit-wrapper"><span class="icn"><i class="fa fa-pencil-square-o" aria-hidden="true" id="edit"></i><i class="fa fa-trash-o" aria-hidden="true" id="delete"></i></span></div>'
        }]
    });