Add table row in jQuery

前端 未结 30 2268
既然无缘
既然无缘 2020-11-21 05:40

What is the best method in jQuery to add an additional row to a table as the last row?

Is this acceptable?

$(\'#myTable\').append(\'

        
30条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 06:10

    jQuery has a built-in facility to manipulate DOM elements on the fly.

    You can add anything to your table like this:

    $("#tableID").find('tbody')
        .append($('')
            .append($('')
                .append($('')
                    .attr('src', 'img.png')
                    .text('Image cell')
                )
            )
        );
    

    The $('') thing in jQuery is a tag object that can have several attr attributes that can be set and get, as well as text, which represents the text between the tag here: text.

    This is some pretty weird indenting, but it's easier for you to see what's going on in this example.

提交回复
热议问题