createElement + createTextNode oneliner?

前端 未结 3 1595
星月不相逢
星月不相逢 2021-02-06 07:45

I\'m adding a few thousand rows to a table so i need the speed of native javascript for this one.

Currently I\'m using:

nThName = document.createElement(         


        
3条回答
  •  无人共我
    2021-02-06 08:25

    This can be done, as an example:

    document.body.appendChild(
        document.createElement('div').appendChild(
            document.createTextNode('hello')
        ).parentNode
    );
    

    JS Fiddle representative demo.

    I think it's just your approach to chaining that was off; given your specific demo code:

    nTr.appendChild(
        document.createElement('th').appendChild(
            document.createTextNode(workers[i].name)
        ).parentNode
    );
    

    The white-space here isn't essential, it's simply to more-clearly show what goes where.

提交回复
热议问题