从JavaScript数组创建HTML表 [英] Create HTML table from javascript array

查看:94
本文介绍了从JavaScript数组创建HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要得到我的页面上的HTML元素的所有类别,将它并将其存储在阵列。从那以后,我想将它写入我的表在id为表,我已经有了DIV。

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.

到目前为止,我有这个code:

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>

您能帮我休息吗?

顺便说一句,HTML元素从modernizr.js有类。

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

PS:在code是纯JS和jQuery的组合。因为我不知道怎么去在纯JS的HTML元素的所有类。任何想法?

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?

推荐答案

如果你想删除的jQuery干脆使用:

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

的jsfiddle 例子显示表。

JSFiddle example which displays the table.

这篇关于从JavaScript数组创建HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆