-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 获取页面上所有的 className 并且去重 #22
Comments
let allClassName = ()=>{
let className = new Set();
let hasClassEl = document.querySelectorAll('[class]');
[].forEach.call(hasClassEl,(v,i)=>{
let classArr = v.getAttribute("class").split(' ');
classArr.map(name => className.add(name));
})
return [...className]
} |
function getClass() {
let classes = [];
Array.from(document.querySelectorAll('[class]')).forEach(node=> {
classes = classes.concat(node.className.split(' '));
});
return Array.from(new Set(classes));
} |
function getClassNames(){
var classes=document.querySelectorAll('[class]'),i;
var classNames=[];
for(i=0;i<classes.length;i++){
classNames=classNames.concat(classes[i].className.split(''));
}
var classArray=Array.from(new Set(classNames))
return classArray;
} |
|
有没有大神带带我学前端 |
[...new Set(Array.from(document.querySelectorAll('[class]')).map(({className: c}) => c).join(' ').split(' '))] |
function allClassName() {
const classNames = [...document.querySelectorAll("[class]")].reduce(
(list, node) => [...list, ...node.classList.values()],
[]
);
return [...new Set(classNames)];
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
最终得到一个数组
实现方式多,但要尽量写的好看一点哦~
The text was updated successfully, but these errors were encountered: