说我有基于任何selector
是否可以在元素数组上使用querySelectorAll?
var elems = document.querySelectorAll(selector);
我不知道什么是包含在elems
,但是让我们假设elems.length > 0
我想使用querySelectorAll(或DOM元素的数组一些等价函数)elems
来查找与其他选择器匹配的所有元素。
// example: find all buttons with class `foo` within `elems` array
var buttons = elems.querySelectorAll("button.foo");
这不会(原因很明显)工作,但我不确定如何做到这一点,否则:(
这是我创造了从接受的答案上班包装@Tibos
// Element.matches wrapper
(function(e){
if (typeof e.matches !== "function") {
e.matches = e.webkitMatchesSelector ||
e.mozMatchesSelector ||
e.msMatchesSelector ||
e.oMatchesSelector ||
e.matchesSelector;
}
})(Element.prototype);
你要问这样的相似: [DEMO](http://jsfiddle.net/ashish41191/RanWJ/) –