Skip to content
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

Open
VaJoy opened this issue Mar 23, 2017 · 7 comments
Open

[js] 获取页面上所有的 className 并且去重 #22

VaJoy opened this issue Mar 23, 2017 · 7 comments

Comments

@VaJoy
Copy link
Member

VaJoy commented Mar 23, 2017

最终得到一个数组
实现方式多,但要尽量写的好看一点哦~

@li2568261
Copy link

li2568261 commented Mar 23, 2017

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]
		}

@akaxiaok
Copy link

function getClass() {
    let classes = [];
    Array.from(document.querySelectorAll('[class]')).forEach(node=> {
            classes = classes.concat(node.className.split(' '));
    });
    return Array.from(new Set(classes));
}

@decadeheart
Copy link

decadeheart commented Mar 23, 2017

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

@aolinpk
Copy link

aolinpk commented Aug 3, 2017

var classes = document.querySelectorAll('[class]');
classes = Array.prototype.slice.call(classes,0); 
var classNames = [];
classes.forEach(function(v,k) {
    classNames = classNames.concat(classes[k].className.split(''));
})
classNames = new Set(classNames);

@ysjhnuitanfei
Copy link

有没有大神带带我学前端

@Jiasm
Copy link
Member

Jiasm commented Apr 13, 2018

[...new Set(Array.from(document.querySelectorAll('[class]')).map(({className: c}) => c).join(' ').split(' '))]

@uinz
Copy link

uinz commented Jul 25, 2018

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
Projects
None yet
Development

No branches or pull requests

8 participants