最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Create HTML table from javascript array - Stack Overflow

programmeradmin3浏览0评论

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 I already have.

So far I have this code:

var string = $('html').attr('class');
var array = string.split(' ');
var arrayLength = parseInt(array.length);

for (i=0; i<=arrayLength; i++) {
    // code
}

<div id="test><!-- table goes here --></div>

Can you help me with the rest?

btw, the HTML element has the classes from a modernizr.js.

PS: The code is combination of pure JS and jQuery. Because I dont know how to get all classes of the HTML element in pure JS. Any Idea?

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 I already have.

So far I have this code:

var string = $('html').attr('class');
var array = string.split(' ');
var arrayLength = parseInt(array.length);

for (i=0; i<=arrayLength; i++) {
    // code
}

<div id="test><!-- table goes here --></div>

Can you help me with the rest?

btw, the HTML element has the classes from a modernizr.js.

PS: The code is combination of pure JS and jQuery. Because I dont know how to get all classes of the HTML element in pure JS. Any Idea?

Share Improve this question asked Mar 3, 2012 at 20:29 user1247294user1247294
Add a comment  | 

3 Answers 3

Reset to default 11

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);

if you have a table already in the html

<div id="test><table >
    </table>
</div>  

you can simply append new rows to it,

var string = $('html').attr('class');
var array = string.split(' ');
var arrayLength = parseInt(array.length);

for (i=0; i<=arrayLength; i++) {
  $("#test table") .append('<tr><td>'+array[i]+'</td></tr>') 
}

It is not clear if you want the class names per row or per column. These examples are one class name per row. Try this:

var elm = $('#test'),
    table = $('<table>').appendTo(elm);

$(document.documentElement.className.split(' ').each(function() {
    table.append('<tr><td>'+this+'</td></tr>');
});

I used native code to get the classNames of the HTML element: document.documentElement.className, but you might as well use $('html').attr('class').

A native JS example using innerHTML:

var d = window.document,
    elm = d.getElementById('test'),
    html = '<table>',
    classes = d.documentElement.classNames.split(' '),
    i = 0;

for(; classes[i]; i++) {
    html += '<tr><td>' + classes[i] + '</td></tr>';
}

elm.innerHTML = html + '</table>;
发布评论

评论列表(0)

  1. 暂无评论