How to use createElement to create a new table?

前端 未结 2 1437
借酒劲吻你
借酒劲吻你 2020-12-18 05:34

Why the following code doesn\'t work?



    

        
2条回答
  •  鱼传尺愫
    2020-12-18 05:53

    To the best of my knowledge, setting the innerHTML property of a table element or table section element (like tbody or thead) does not work on Internet Explorer (EDIT: I just checked - with ietester and plain IE8. Result is "unknown runtime error" for IE6 and IE8, and it crashes IE7 but that might be an IEtester specific problem).

    The DOM standard way of adding rows to a table is using the insertRow() method on a table or table section element (look for HTMLTableElement and HTMLTableSectionElement in that DOM spec) :

    
    

    In the script, there is no explicit table section being created. AFAIK, a TBODY is automatically created, and rows are inserted in there.

    EDIT: regarding IE, I should point out that you can add a table with content and all by setting the innerHTML property, but the html you inject in there must be a complete table. So this does work, even on IE:

    
    

提交回复
热议问题