复杂的JQuery选择器-如何选择一个任意'代'
Complex JQuery selector - how to select one arbitrary 'generation'?
我们使用的框架可以创建由属性data-type=[some data type]
标识的任意嵌套的HTML数据元素。其中的每一个都可以包含直接输入字段以及其他data-type
,可以是单重态或数组。嵌套结构的唯一优点是data-type
在任何深度都不会包含相同类型的data-types
。
HTML I必须使用
<div data-type='project' id='example1'>
<input name='start-date'/>
<div data-type='project-lead' id='example2'>
<input name='department'/>
<input name='email'/>
<div data-type='analyst'>
<input name='department'/>
<input name='email'/>
</div>
<div data-type='analyst'>
<input name='department'/>
<input name='email'/>
</div>
<div data-type='analyst'>
<input name='department'/>
<input name='email'/>
</div>
</div>
<div class="JustToMakeMyLifeMoreDifficult">
<div data-type='sponsor'>
<input name='department'/>
<input name='email'/>
</div>
<div data-type='sponsor'>
<input name='department'/>
<input name='email'/>
</div>
</div>
</div>
选择器问题
我需要一个JQuery查找选择器,它可以在给定对象下一个data-type
级别获得一组data-type
元素
myData($obj){
return $obj.find('[data-type]').not([data-type elements further down]);
}
这样:
myData($('#example1'))
myData($('#example2'))
分别产生jquery结果:
[project-lead,sponsor,sponsor]
[analyst, analyst, analyst]
JQuery向导,请帮帮我。你是唯一能做到的人。
已回答
这在JQuery选择器中是不可能的。我将下面Patrick非常优雅的解决方案封装成一个通用的JQuery函数-
(function( $ ){
$.fn.dataChildren = function(_selector) {
var iter = this;
var res = this.children(_selector);
while ( ( iter = iter.children(':not(' + _selector +')') ).length ) {
res = res.add(iter.children(_selector));
}
return res;
};
})( jQuery );
因此:
$('#example1').dataChildren('[data-type]')
工程如上所述。I<3 SO
编辑2:我想这就是您想要的:
var el = $('#example1');
var res = el.children('[data-type]');
while ( ( el = el.children(':not([data-type])') ).length ) {
res = res.add(el.children('[data-type]'));
}
这个递归深入,但当发现具有data-type
的元素时,任何子分支上的递归都会停止,所以它只在不具有data-type
的子分支存在时才继续。
如果我使用do-while
循环,并展开一些代码,可能会更容易理解:
var el = $('#example1'); // el is the current level
var res = $(); // res holds the result
var data_types; // holds the children with data-type for the current level
do {
// from current level, get children with data-type
data_types = el.children('[data-type]');
// add those to the result set
res = res.add( data_types );
// make the current level be the children of the current level that
// do NOT have data-type
el = el.children().not( data_types );
} while( el.length ); // continue as long as the new current level
// has at least one element
编辑:我可能误解了其中一部分
看起来具有data-type
的元素可能具有也具有data-type
的子元素。如果是这种情况,请将选择器更改为:
var ex = $('#example1');
var res = ex.find('> [data-type], > * > [data-type]');
综上所述,它说获取所有具有data-type
属性的子和孙。
原始答案:
如果我知道你想要data-type
的孩子,而不是data-type
的孩子,你也需要添加他们的data-type
的孩子。
var ex = $('#example1');
var res = ex.find('> [data-type], > :not([data-type]) > [data-type]');
这使用multiple-selector
[docs]。
第一个选择器是:
'> [data-type]'
这将得到具有CCD_ 17属性的子代。
第二个选择器是:
'> :not([data-type]) > [data-type]'
它将首先让不的孩子拥有data-type
,但在这些孩子中,它将让拥有data-type
。
这看起来像你想要的吗?
一个POJS函数可以执行您想要的操作并返回匹配元素的数组,它是:
function getNodes(id) {
var el = (typeof id == 'string')? document.getElementById(id) : id;
var result = [];
var node, nodes = el.childNodes;
var prop = 'data-type';
var tag = 'div';
for (var i=0, iLen=nodes.length; i<iLen; i++) {
node = nodes[i];
if (node.tagName && node.tagName.toLowerCase() == tag) {
if (node.getAttribute(prop)) {
result.push(node);
} else {
result = result.concat(getNodes(node));
}
}
}
return result;
}
然而,我不确定你是否希望它像这次那样深入。它可以被修改为只到达一定的深度(比如一两个级别)。
此
$obj.find('> [data-type]');
如果我把你的问题解决对了,应该行得通。
选择器'> [data-type]'
的意思是"给我所有定义了‘数据类型’并且是容器的直接子级的元素。
$obj.children().map(function () {return this.name;})
也许吧?
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- 一个html/javascript'小工具'知道用户何时单击了小部件外的任意位置
- 匹配三个正则表达式中的任意一个,以先到的为准(在javascript中)
- Visual Studio 2010跳转到下一个任意字符/字符串
- 一个控制器如何在 Ember .js 中观察任意控制器/模型上的事件
- 单击任意菜单按钮返回一个寻呼机并向下滚动到位置
- count'最后一个孩子'从javascript对象/数组结构中的任意点
- Javascript:正则表达式,用于匹配任意序列中的8位数字、一个字符和一个hypen
- 有没有一种更简洁的方法来设置一个条件语句,以便为变量的任意值集执行
- 编写一个可任意调用次数的curried javascript函数,该函数在最后一次函数调用时返回值
- 将Class添加到具有任意2个类的所有表行,并将另一个样式添加到具有任何3个类的全部表行
- 将JavaScript中数组值的位置从任意更改为最后一个
- 如何让用户输入两个字段中的任意一个
- 如何找到一个集合项目的任意属性,但不是_id,在流星
- Javascript -创建一个正则表达式,在数组中获取所有带有任意ID的标签
- 正则表达式:匹配两个条件中的任意一个
- 如何将任意数量的参数传递给JavaScript中另一个函数的参数
- 在任意滑动条下面添加一个文本框
- 如果我们使用new array (len).fil({})创建对象数组;然后在任意一个对象中添加一个键,它会被反射到所有
- 复杂的JQuery选择器-如何选择一个任意'代'