查找所有可见的选项与4个选项
什么是最短的方式获得所有selects正好4 options?查找所有可见的选项与4个选项
否则我将不得不每次循环select并获得$find('option').length == 4
这似乎并不奏效:
$.grep($("select:visible"), function() {     return $(this).find('option').length == 4; 
}); 
回答:
您可以使用:has(),:eq()和:not()选择组合。
表达:has(option:eq(3))将针对其具有交替 - 以免4 option子元件和表达:not(:has(option:eq(4)))将排除具有5 option子元素的元素。
$("select:visible:has(option:eq(3)):not(:has(option:eq(4)))").css('color', 'red')<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <select>  
    <option>1</option>  
    <option>2</option>  
    <option>3</option>  
</select>  
<select>  
    <option>1</option>  
    <option>2</option>  
    <option>3</option>  
    <option>4</option>  
</select>  
<select>  
    <option>1</option>  
    <option>2</option>  
    <option>3</option>  
    <option>4</option>  
    <option>5</option>  
</select>
您还可以使用.filter()
$("select:visible").filter(function() {      return $(this).find('option').length == 4;  
}).css('color', 'red')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <select>  
    <option>1</option>  
    <option>2</option>  
    <option>3</option>  
</select>  
<select>  
    <option>1</option>  
    <option>2</option>  
    <option>3</option>  
    <option>4</option>  
</select>  
<select>  
    <option>1</option>  
    <option>2</option>  
    <option>3</option>  
    <option>4</option>  
    <option>5</option>  
</select>
回答:
您的代码也是正确的唯一的问题是你需要用你的S使用某些元素选择元素可以使$ .find函数起作用。 例如
//Going to add a div to wrap all elememts  var myDiv = $("<div><select><option>1</option><option>2</option><option>3</option></select><select><option>1</option><option>2</option><option>3</option><option>4</option></select><select><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select><div>"); 
$.grep(myDiv.find("select"),(el)=>$(el).find("option").length===4); 
以上是 查找所有可见的选项与4个选项 的全部内容, 来源链接: utcz.com/qa/265059.html



![[奥日与萤火意志]全收集指南攻略详解](/wp-content/uploads/thumbs/451668_thumbnail.jpg)
