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.
我想在我的页面上获取HTML元素的所有类,将其拆分并将其存储在数组中。之后我想将它写入div中的表格,其中包含我已经拥有的id“table”。
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.
顺便说一下,HTML元素有来自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?
PS:代码是纯JS和jQuery的组合。因为我不知道如何在纯JS中获取HTML元素的所有类。任何想法?
3 个解决方案
#1
10
If you're trying to remove jQuery altogether use this:
如果您尝试删除jQuery,请使用以下命令:
// 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);
#2
2
if you have a table already in the html
如果你已经在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>')
}
#3
2
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')
.
我使用本机代码来获取HTML元素的classNames:document.documentElement.className,但您也可以使用$('html')。attr('class')。
A native JS example using innerHTML:
使用innerHTML的原生JS示例:
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>;
#1
10
If you're trying to remove jQuery altogether use this:
如果您尝试删除jQuery,请使用以下命令:
// 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);
#2
2
if you have a table already in the html
如果你已经在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>')
}
#3
2
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')
.
我使用本机代码来获取HTML元素的classNames:document.documentElement.className,但您也可以使用$('html')。attr('class')。
A native JS example using innerHTML:
使用innerHTML的原生JS示例:
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>;