最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pure JS, get element by class but not class - Stack Overflow

programmeradmin1浏览0评论
var items = document.getElementsByClassName("classname");

Gives me all the .classname classes, how do I update the code to get all those classes but excluding .classname_exclude?

var items = document.getElementsByClassName("classname");

Gives me all the .classname classes, how do I update the code to get all those classes but excluding .classname_exclude?

Share Improve this question edited Jan 24, 2016 at 21:22 madox2 52k21 gold badges104 silver badges101 bronze badges asked Jan 24, 2016 at 21:21 PumychPumych 1,3983 gold badges19 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Rather than using the method .getElementsByClassName(), you could use the method .querySelectorAll() (which accepts CSS3 selectors) and use the :not() pseudo class to negate those elements:

var items = document.querySelectorAll('.classname:not(.classname_exclude)');
发布评论

评论列表(0)

  1. 暂无评论