如果自定义选择器大于jQuery中的数字,则进行筛选
快给你一个。我查看了jQuery.com中的选择器,找不到一种按大于一个数字的选择器进行筛选的方法?我想这样做:如果自定义选择器大于jQuery中的数字,则进行筛选,jquery,Jquery,快给你一个。我查看了jQuery.com中的选择器,找不到一种按大于一个数字的选择器进行筛选的方法?我想这样做: $("[level>'4']").hide(); 我的html如下所示: <div id="1" level="4">Test</div> 测试 如何使用该语法或类似语法隐藏所有大于4的div?4 $("[level]").filter(function () { return parseInt($(this).attr('level'),
$("[level>'4']").hide();
我的html如下所示:
<div id="1" level="4">Test</div>
测试
如何使用该语法或类似语法隐藏所有大于4的div?4
$("[level]").filter(function () {
return parseInt($(this).attr('level'), 10) > 4;
}).hide();
只需这样做:
$('[level]').each(function(){
var $this=$(ths);
var level = parseInt($this.attr('level'), 10);
if (level>4) $this.hide();
});
我也需要这个特殊的功能,检查是在选择器中完成的,而不是作为另一个嵌入式功能。也许我做的比我需要的更多,但这意味着我现在可以使用选择器过滤基于日期的内容,而不必在每次过滤调用中写入内容 我写的东西使用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的初始代码(我根本没有做过任何广泛的测试,但到目前为止它对我有效):
(函数($){
//单功能完成所有起重作业
函数属性LTSelector(模式、对象、元){
变量args,
objAttr,
checkAttr,
输出=假;
如果(元的类型==='object'){
args=meta;
}否则{
if(meta.match(',')){
args=meta.split(/[“'\s]*,[“'\s]*/);
}否则{
args=[meta];
}
}
objAttr=parseInt($(obj).attr(args[0]),10);
checkAttr=parseInt(args[1],10);
开关(模式){
案例“lt”:
如果(objAttr=checkAttr)输出=true;
打破
}
if(window.console)if(console.log)console.log('attrgtlselector',objAttr,mode,checkAttr,output);
返回输出;
}
//向jQuery添加自定义伪选择器
$.expr[':'].attrLT=函数(对象、索引、元、堆栈){
返回attrGTLTSelector('lt',obj,meta[3]);
};
$.expr[':'].attrGT=函数(对象、索引、元、堆栈){
返回attrGTLTSelector('gt',obj,meta[3]);
};
$.expr[':'].attrLTEq=函数(对象、索引、元、堆栈){
返回attrGTLTSelector('lte',obj,meta[3]);
};
$.expr[':'].attrGTEq=函数(对象、索引、元、堆栈){
返回attrGTLTSelector('gte',obj,meta[3]);
};
}(jQuery));
>>您最好使用HTML5数据-
属性,而不是非标准的html属性。例如,data level=“4”
而不是level=“4”
。
// 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)'
});
(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));