Create HTML table from javascript array

前端 未结 3 1865
野趣味
野趣味 2020-12-31 19:00

I want to get all classes of the HTML element on my page, split it and store it in array. After that I want to write it into my table in the div with the id \"table\" which

3条回答
  •  悲哀的现实
    2020-12-31 19:53

    If you're trying to remove jQuery altogether use this:

    // Get array of classes without jQuery
    var array = document.getElementsByTagName('html')[0].className.split(/\s+/);
    
    var arrayLength = array.length;
    var theTable = document.createElement('table');
    
    // Note, don't forget the var keyword!
    for (var i = 0, tr, td; i < arrayLength; i++) {
        tr = document.createElement('tr');
        td = document.createElement('td');
        td.appendChild(document.createTextNode(array[i]));
        tr.appendChild(td);
        theTable.appendChild(tr);
    }
    
    document.getElementById('table').appendChild(theTable);
    

提交回复
热议问题