试试这个:
$('select:empty:first');
但要注意的是,上面的选择器只有在你的select
盒子像
<select></select> // even without any newline
因为:empty
没有子元素的点元素,而不是带有换行符或文本节点的事件。
所以如果你select
看起来像:
<select>
</select>
上面的选择器将失败。要获得select
两种类型,您可以使用
$('select').filter(function() {
return !this.innerHTML.replace(/\s/g,'').length;
}).first();
或如@gdoron 提到的
$('select').filter(function() {
return !$.trim(this.innerHTML);
}).first();
在我的选择中,第二个是可靠的。
// solution to your recursive search
$('select')
.filter(function() { // filtering for empty select
return !this.innerHTML.replace(/\s/g,'').length;
})
.first() // taking the first
.addClass('lastIndentified'); // adding a class to keep track
$('button#search').on('click', function() {
// reference for last empty select
var lastIndentified = $('select.lastIndentified');
lastIndentified
.nextAll('select') // searching for all select
.filter(function() { // making filtering
return !this.innerHTML.replace(/\s/g,'').length;
})
.first() // taking first one from lastIndetified
.addClass('lastIndentified');
lastIndentified.removeClass('lastIndentified'); // remove class from last empty select and pass it to new empty select
// for example
// calling function to process with last empty select
processWithLastEmptySelect($('select.lastIndentified'));
});
function processWithLastEmptySelect(lastSelect) {
// your code if needed
lastSelect.css('border', '1px solid green');
}
工作演示