计数选择选项是简单
$('.myselect option').length;但是有没有一个速记选择器来过滤大小呢?做这样的事?
$('.myselect').childCountBetween('option', 2, 4).hide();ie在所有的.myselect上运行一个jquery方法(隐藏在这个实例中),其中包含2到4个选项。
编辑:为了澄清-我希望作为选择器,过滤“父”元素的基础上的子计数。Ie不想隐藏选项2-4,而是隐藏包含2到4个项的字段。类似的任务是在ULs中添加一个类,其中包含2-4Lis等。如果原来的问题含糊不清,很抱歉。
发布于 2013-11-07 04:43:44
不确定这是你想要的解决方案,但你所描述的应该有效.
$.fn.childCountBetween = function (child, min, max) {
return $(this).filter(function (id, el) {
var children = $(el).find(child).length;
return children > min && children < max;
});
};
$('.myselect').childCountBetween('option', 2, 4).hide();发布于 2013-11-07 04:34:57
试着使用切片(),
$('.myselect option').slice( 2, 4 ).hide();注意事项:IE不允许您操作option elements directly。为此,您必须从select和remove all对option elements进行repopulate。
发布于 2013-11-07 04:35:43
您还可以使用jQuerys eq()函数来查找特定的索引。
http://api.jquery.com/eq/
https://stackoverflow.com/questions/19828031
复制相似问题