Add table row in jQuery

前端 未结 30 2265
既然无缘
既然无缘 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:11

    I know you have asked for a jQuery method. I looked a lot and find that we can do it in a better way than using JavaScript directly by the following function.

    tableObject.insertRow(index)
    

    index is an integer that specifies the position of the row to insert (starts at 0). The value of -1 can also be used; which result in that the new row will be inserted at the last position.

    This parameter is required in Firefox and Opera, but it is optional in Internet Explorer, Chrome and Safari.

    If this parameter is omitted, insertRow() inserts a new row at the last position in Internet Explorer and at the first position in Chrome and Safari.

    It will work for every acceptable structure of HTML table.

    The following example will insert a row in last (-1 is used as index):

    
        
            
        
    
               
            
    cell 1 cell 2
    cell 3 cell 4

    I hope it helps.

提交回复
热议问题