有没有办法转换:$("#first").find("input").not("td td input")
(http://jsfiddle.net/4K9TG/)
...进入querySelectorAll选择器?
(从jQuery: find() children until a certain threshold element is encountered继续)
答案 0 :(得分:1)
not
只是对满足条件的集合的过滤器。看看这是否有帮助。
var query = function(selector, element) {
element = element || document;
return [].slice.call(element.querySelectorAll(selector));
};
var not = function(selector) {
var exclude = query(selector);
return function(element) {
return exclude.indexOf(element) == -1;
};
};
var inputs = query('#first input').filter(not('td td input'));
以下是演示:http://jsbin.com/EJAyiSux/1/edit
您可以将大多数jQuery方法实现为具有高阶函数的简单filter
和map
序列,如上所示。毕竟,jQuery集合只是元素的数组。
答案 1 :(得分:0)
我最终做了一个这样做的功能。
var n = get_elements_until (document.getElementById('first'), 'input', 'td')
console.log (n)
function get_elements_until (parent, tagname_to_search_for, tagname_to_stop_at) {
var element_list = []
var stack_current = [parent]
while (true) {
var stack_new = []
for (var s = 0, curlen_s = stack_current.length; s < curlen_s; s++) {
var children = stack_current[s].childNodes
for (var i = 0, curlen = children.length; i < curlen; i++) {
var child = children[i], tagname = child.tagName
if (typeof tagname == "undefined") continue
tagname = tagname.toLowerCase ()
if (tagname == tagname_to_search_for) element_list.push (child)
if (tagname != tagname_to_stop_at) stack_new.push (child)
}
}
stack_current = stack_new
if (stack_new.length == 0) break
}
return element_list
}