Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b380bf7

Browse files
committed
feat(remove): remove elements using querySelectorAll
1 parent cb3463c commit b380bf7

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

‎remove-all-but.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Removes all elements except for the trees rooted
3-
in the given selectors.
3+
in the given selectors. Selectors are queried using querySelectorAll
44
55
For example, given a document
66
@@ -20,25 +20,24 @@ body
2020
hello
2121
*/
2222
(function hideAllBut() {
23-
function toArray(what) {
24-
return Array.prototype.slice.call(what, 0);
25-
}
26-
const selectors = toArray(arguments);
23+
'use strict';
24+
25+
const selectors = Array.from(arguments);
2726
if (!selectors.length) {
2827
throw new Error('Need at least one selector to leave');
2928
}
3029

31-
const keep = selectors.map(function (selector) {
32-
return document.querySelector(selector);
33-
});
30+
const keep = selectors.reduce(function (all,selector) {
31+
return all.concat(Array.from(document.querySelectorAll(selector)));
32+
},[]);
3433

3534
function shouldKeep(el) {
3635
return keep.some(function (keepElement) {
3736
return keepElement.contains(el) || el.contains(keepElement);
3837
});
3938
}
4039

41-
const all = toArray(document.body.querySelectorAll('*'),0);
40+
const all = Array.from(document.body.querySelectorAll('*'));
4241
var removed = 0;
4342

4443
all.forEach(function (el) {
@@ -49,4 +48,4 @@ body
4948
});
5049

5150
console.log('removed %d elements', removed);
52-
}('.foo','#baz'));
51+
}('.foo'));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /