如何根据其中子项的文本值获取单行元素?例如,如果我有以下内容:
<div class="row">
<div class="a">AA</div>
<div class="b">B</div>
<div class="c">C</div>
</div>
<div class="row">
<div class="a">AA</div>
<div class="b">BB</div>
<div class="c">CC</div>
</div>
<div class="row">
<div class="a">AAA</div>
<div class="b">BB</div>
<div class="c">CC</div>
</div>
如何根据具有带 AA 的子类“a”div 和带 BB 的“b”类 div 以及带 CC 的“c”类 div 的行获取第二行?
var elem = $('.row'); // How can I get a particular row based on the text values of the children elements in it?
最佳答案
Reduce the set of matched elements to those that match the selector or pass the function's test.
var elem = $('.row').filter(function(){
var _this = $(this);
var hasA = _this.has('div.a');
var hasB = _this.has('div.b');
var hasC = _this.has('div.c');
if(hasA && hasB && hasC){
return _this.find('div.a').text().trim() == 'AA'
&& _this.find('div.b').text().trim() == 'BB'
&& _this.find('div.c').text().trim() == 'CC'
}
return false;
});
根据要求
function getElements(valueForA, valueForB, valueForC){
return $('.row').filter(function(){
var _this = $(this);
var hasA = _this.has('div.a');
var hasB = _this.has('div.b');
var hasC = _this.has('div.c');
if(hasA && hasB && hasC){
return _this.find('div.a').text().trim() == valueForA
&& _this.find('div.b').text().trim() == valueForB
&& _this.find('div.c').text().trim() == valueForC
}
return false;
});
}
关于javascript - 根据其中子元素的文本值获取单行元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34895757/