解决方法
这应该工作,它不需要jquery.
var used = []; var elements = null; //get all elements if (typeof document.getElementsByTagName != 'undefined') { elements = document.getElementsByTagName('*'); } if (!elements || !elements.length) { elements = document.all; // ie5 } //loop over all element for (var i = 0; i < elements.length; i++){ //loop over element's classes var classes = elements[i].className.split(' '); for (var j = 0; j < classes.length; j++) { var name = classes[j]; //add if not exists if (name.length > 0 && used.indexOf(name) === -1) { used.push(name); } } } alert(used.join(' '));
更实用的方法
var elements = document.getElementsByTagName('*'); var unique = function (list,x) { if (x != "" && list.indexOf(x) === -1) { list.push(x); } return list; }; var trim = function (x) { return x.trim(); }; var classes = [].reduce.call(elements,function (acc,e) { return e.className.split(' ').map(trim).reduce(unique,acc); },[]);