Javascript选择具有最大值类的元素
我知道有很多关于Jquery选择器和正则表达式的主题,但我需要的是 我有一些像这样的部门:Javascript选择具有最大值类的元素,javascript,jquery,jquery-selectors,jquery-filter,Javascript,Jquery,Jquery Selectors,Jquery Filter,我知道有很多关于Jquery选择器和正则表达式的主题,但我需要的是 我有一些像这样的部门: <div class="some random things rank0">hello</div> <div class="some things rank1">hello</div> <div class="things rank1">hello</div> <div class="some random rank2 thin
<div class="some random things rank0">hello</div>
<div class="some things rank1">hello</div>
<div class="things rank1">hello</div>
<div class="some random rank2 things">hello</div>
<div class="random rank2 some">hello</div>
<div class="some things rank3">hello</div>
<div class="some random rank4">hello</div>
预期结果:
<div class="some random things rank0">hello</div>
<div class="some things rank1">hello</div>
<div class="things rank1">hello</div>
<div class="some random rank2 things">hello</div>
<div class="random rank2 some">hello</div>
<div class="some things rank3">test</div>
<div class="some random rank4">test</div>
你好
你好
你好
你好
你好
测试
测试
我使用了简单的JQuery/Javascript代码来过滤结果。我的正则表达式也不太好:
var特定值=2;
$.each($(“div”).filter(函数(){
var classes=$(this.attr(“class”).split(“”);
var matchfound=false;
对于(i=0;i-1){
var秩=类别[i];
秩=秩。替换(“秩”,“秩”);
秩=parseInt(秩,10);
if(等级>特定值)
matchfound=true;
}
}
返回匹配发现;
}),函数(){
$(this.html(“test”);
})
你好
你好
你好
你好
你好
你好
您好
您可以使用正则表达式来检查rank
类的存在性并获取它的值
var特定值=2;
$(“div”).filter(函数(){
var match=/rank([\d]+)/g.exec($(this.attr(“class”));
if(match!=null&&match[1]>特定值)
返回true;
}).文本(“文本”)代码>
你好
你好
你好
你好
你好
你好
您好
一种方法是以下方法,它首先为每个相关元素分配一个有效的自定义属性,数据秩
,以便于后续选择/过滤,然后使用函数返回找到的元素的jQuery集合:
// find all elements with the string 'rank' present in the
// 'class' attribute, then iterating over that collection
// using the each() method
$('[class*=rank]').each(function() {
// while we only expect a single rank from from any element,
// the singular ('class') is a reserved word, so here we use
// the plural ('classes').
// First we convert the Array-like classList (a collection of
// the current element's class-names) into an Array, in order
// to use Array.prototype.filter():
var classes = Array.from(this.classList).filter(function(cName) {
// here we return only those class-names that begin with ('^')
// the string 'rank' ('rank') followed by a sequence of
// one-or-more ('+') number characters ('\d') which is followed
// by a word-break ('\b'), using RegExp.prototype.test() which
// returns Boolean true if the supplied string (the current
// class-name) matches the regular expression:
return /^rank\d+\b/.test(cName);
});
// if the classes Array has a truthy length (1 or above):
if (classes.length) {
// we find the numbers ('\d+'), at the end of the
// end of the String ('$'), and set that number as
// the data-rank attribute (the dataset.rank property):
this.dataset.rank = classes[0].match(/\d+$/);
}
});
// here we supply the collection of elements we wish to
// select from, and the rank above-which we wish to select:
function selectByRank(collection, rank) {
// we return the filtered jQuery collection, having
// filtered it with the jQuery filter() method:
return collection.filter(function(){
// we retain those elements for which the data-rank
// attribute, when interpreted as a base-10 number
// is greater than the supplied rank:
return parseInt(this.dataset.rank, 10) > rank;
});
}
// because we return a jQuery collection we can apply
// jQuery methods directly to the returned element
// collection:
selectByRank($('[class*=rank]'), 2).text('test').css('color', 'limegreen');
$('[class*=rank]')。每个(函数(){
var classes=Array.from(this.classList).filter(函数(cName){
返回/^rank\d+\b/.test(cName);
});
if(类.长度){
this.dataset.rank=classes[0]。匹配(/\d+$/);
}
});
函数selectByRank(秩){
返回$('[class*=rank]')。筛选器(函数(){
返回parseInt(this.dataset.rank,10)>rank;
});
}
选择数据库(2).text('test').css('color','limegreen')代码>
你好
你好
你好
你好
你好
你好
您好
最后两个div在处理后有了新内容,我不理解您的评论谢谢,它可以工作。您可以更改您的两行rank=rank.replace(“rank”,”);秩=parseInt(秩,10)代码>进入rank=rank.substring(4)代码>。
// find all elements with the string 'rank' present in the
// 'class' attribute, then iterating over that collection
// using the each() method
$('[class*=rank]').each(function() {
// while we only expect a single rank from from any element,
// the singular ('class') is a reserved word, so here we use
// the plural ('classes').
// First we convert the Array-like classList (a collection of
// the current element's class-names) into an Array, in order
// to use Array.prototype.filter():
var classes = Array.from(this.classList).filter(function(cName) {
// here we return only those class-names that begin with ('^')
// the string 'rank' ('rank') followed by a sequence of
// one-or-more ('+') number characters ('\d') which is followed
// by a word-break ('\b'), using RegExp.prototype.test() which
// returns Boolean true if the supplied string (the current
// class-name) matches the regular expression:
return /^rank\d+\b/.test(cName);
});
// if the classes Array has a truthy length (1 or above):
if (classes.length) {
// we find the numbers ('\d+'), at the end of the
// end of the String ('$'), and set that number as
// the data-rank attribute (the dataset.rank property):
this.dataset.rank = classes[0].match(/\d+$/);
}
});
// here we supply the collection of elements we wish to
// select from, and the rank above-which we wish to select:
function selectByRank(collection, rank) {
// we return the filtered jQuery collection, having
// filtered it with the jQuery filter() method:
return collection.filter(function(){
// we retain those elements for which the data-rank
// attribute, when interpreted as a base-10 number
// is greater than the supplied rank:
return parseInt(this.dataset.rank, 10) > rank;
});
}
// because we return a jQuery collection we can apply
// jQuery methods directly to the returned element
// collection:
selectByRank($('[class*=rank]'), 2).text('test').css('color', 'limegreen');