快速为您服务。我查看了jQuery.com中的选择器,找不到按大于数字的选择器进行过滤的方法?我想这样做:
$("[level>'4']").hide();
我的HTML看起来像这样:
<div id="1" level="4">Test</div>
如何使用该语法或类似语法隐藏所有大于4的div?4
答案 0 :(得分:5)
尝试以下,
$("[level]").filter(function () {
return parseInt($(this).attr('level'), 10) > 4;
}).hide();
答案 1 :(得分:3)
$('div').filter(function(){
return parseInt($(this).attr('level')) > 4);
});
答案 2 :(得分:1)
只需这样做:
$('[level]').each(function(){
var $this=$(ths);
var level = parseInt($this.attr('level'), 10);
if (level>4) $this.hide();
});
答案 3 :(得分:1)
我也需要这个特殊的功能,在选择器中完成检查而不是另一个嵌入式功能。也许我做的比我需要的多,但这意味着我现在可以使用Selector过滤Isotope的基于日期的东西,而不必在每个过滤器调用中编写内容。
我写的是使用jQuery的伪选择器。
// Hides elements with the attribute "index" that is greater than 4
$(':attrGT("index",4)').hide();
// Filter elements using Isotope with the attribute "data-starttime" that is less than or equal to 1234567890
$container.isotope({
filter: ':attrLTEq("data-starttime",1234567890)'
});
这是版本1的初始代码(我还没有完成任何广泛的测试,但到目前为止它对我有用):
(function ($) {
// Single function to do all the heavy lifting
function attrGTLTSelector(mode, obj, meta) {
var args,
objAttr,
checkAttr,
output = false;
if (typeof meta === 'object') {
args = meta;
} else {
if (meta.match(',')) {
args = meta.split(/["'\s]*,["'\s]*/);
} else {
args = [meta];
}
}
objAttr = parseInt($(obj).attr(args[0]), 10);
checkAttr = parseInt(args[1], 10);
switch (mode) {
case 'lt':
if (objAttr<checkAttr) output = true;
break;
case 'lte':
if (objAttr<=checkAttr) output = true;
break;
case 'gt':
if (objAttr>checkAttr) output = true;
break;
case 'gte':
if (objAttr>=checkAttr) output = true;
break;
}
if (window.console) if (console.log) console.log('attrGTLTSelector', objAttr, mode, checkAttr, output);
return output;
}
// Add custom pseudo selectors to jQuery
$.expr[':'].attrLT = function(obj, index, meta, stack){
return attrGTLTSelector('lt', obj, meta[3]);
};
$.expr[':'].attrGT = function(obj, index, meta, stack){
return attrGTLTSelector('gt', obj, meta[3]);
};
$.expr[':'].attrLTEq = function(obj, index, meta, stack){
return attrGTLTSelector('lte', obj, meta[3]);
};
$.expr[':'].attrGTEq = function(obj, index, meta, stack){
return attrGTLTSelector('gte', obj, meta[3]);
};
}(jQuery));