我怎样才能选择“最浅”匹配后裔? [英] How can I select the "shallowest" matching descendant?
问题描述
如何选择第一个最浅的输入?
我目前的选择将被标记为已选择的div。
我赢了不知道会有多少个关卡。
< div class =selected> <! - 已经有这个 - >
< div class =未知数量的包装面板>
...
< div class =collection>
< div class =child>
< input type =textvalue =2/> <! - 不要这样 - >
< / div>
< / div>
< input type =textvalue =2/> <! - 需要这个 - >
< input type =textvalue =2/>
...
< / div>
< / div>
好像是 find()。first()
给了我最深的一个。
为清晰起见编辑。我需要根据它是浅的这一事实来找到它,而不是基于其他的独特属性。
这可能类似于<$ c如果我理解你的问题,你需要递归检查。如果我明白你的问题,你需要递归检查
函数findShallowest(root,sel){
var children = root。儿童();
if(children.length){
var matching = children.filter(sel);
if(matching.length){
return matching.first();
} else {
return findShallowest(children,sel);
}
} else {
return null;
}
}
var selected = $('。selected');
findShallowest(选中,':text');
示例: http://jsfiddle.net/Qf2GM/ 编辑:忘记了一个return语句,并且有一个ID选择符而不是初始 或者将其制作成您自己的自定义插件: 示例: / strong> http://jsfiddle.net/qX94u/ How can I select the first "shallowest" input? My current selection will be the div marked "selected". It seems like Edited for clarity. I need to find it based on the fact that it is shallower, not based on other unique attributes. This might be like a reverse of If I understand your issue, you need to recursively check the child nodes for elements with that class. Example: http://jsfiddle.net/Qf2GM/ EDIT: Had forgotten a return statement, and had an ID selector instead of a class selector for the initial Or make it into your own custom plugin: Example: http://jsfiddle.net/qX94u/
这篇关于我怎样才能选择“最浅”匹配后裔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
.selected
的类选择器。
(function($){
$ .fn.findShallowest = function(sel){
return findShallowest(this,sel);
};
函数findShallowest(root,sel){
var children = root.children();
if(children.length){
var matching = children.filter(sel );
if(matching.length){
返回matching.first();
} else {
返回n findShallowest(children,sel);
}
} else {
return $();
}
}
})(jQuery);
var result = $('。selected')。findShallowest(':text');
alert(result.val());
I won't know how many levels down it will be.<div class="selected"> <!-- already have this -->
<div class="unknown-number-of-wrapper-panels">
...
<div class="collection">
<div class="child">
<input type="text" value="2" /> <!-- don't want this -->
</div>
</div>
<input type="text" value="2" /> <!-- need this -->
<input type="text" value="2" />
...
</div>
</div>
find().first()
gives me the deepest one.closest()
?function findShallowest( root, sel ) {
var children = root.children();
if( children.length ) {
var matching = children.filter( sel );
if( matching.length ) {
return matching.first();
} else {
return findShallowest( children, sel );
}
} else {
return null;
}
}
var selected = $('.selected');
findShallowest( selected, ':text' );
.selected
.
(function($) {
$.fn.findShallowest = function( sel) {
return findShallowest( this, sel );
};
function findShallowest(root, sel) {
var children = root.children();
if (children.length) {
var matching = children.filter(sel);
if (matching.length) {
return matching.first();
} else {
return findShallowest(children, sel);
}
} else {
return $();
}
}
})(jQuery);
var result = $('.selected').findShallowest( ':text' );
alert( result.val() );