8

Here's my goal: do something on an element, an <optgrooup>, if all of its children are invisible.

My code below outlines the in red if it has any invisible children. But I want to do so only if all the children are invisible. If the element has any children that are visible, then don't highlight it.

How can I tweak the jQuery selector to do that?

Thanks in advance.

<select multiple="multiple" name="availableInstanceId" id="availableInstanceId">
<optgroup label="Option Group 1">
   <option >visible item 1</option>
   <option >visible item 2</option>
</optgroup>
<optgroup label="Option Group 2 - Should be highlighted">
   <option style="display:none;">invisible A</option>
   <option style="display: none">invisible B</option>
</optgroup>

<optgroup label="Option Group 3 - Should not be highlighted">
  <option >visible C</option>
  <option style="display: none">invisible D</option>
</optgroup></select>

<script type="text/javascript">
var filterOptions = function(e) {
  // Goal: highlight the <optgroup>'s that have *only* invisible children
  $( '#availableInstanceId > * > *:hidden').parent().css("border","3px solid red");
} 
$(document).ready(function() {
  filterOptions();
});
</script>

Screenshot of image here: http://img144.imageshack.us/img144/556/selectexample.gif

4

6 回答 6

13

假设您要排除没有子元素的元素:

 $(":has(*):not(:has(:visible))")

工作示例。

更新:这比我原来的答案有更好的性能:

$(":hidden").parent().not( $(":visible").parent() )
于 2009-05-08T19:39:51.643 回答
2

This has much better performance than my original answer:

$(":hidden").parent().not( $(":visible").parent() )
于 2009-05-08T20:07:22.123 回答
1

两条线怎么做?一个为每个元素打开它,一个为每个有可见孩子的元素再次关闭它?

$('#availableInstanceId > *').css("border","3px solid red");
$('#availableInstanceId > * > *:visible').parent().css("border","none");
于 2009-05-08T19:39:21.380 回答
1

归功于Jed Schmidt。以下代码适用于 IE8。

请注意,尽管有样式,IE8 实际上并没有隐藏<option>元素。display: noneIE8 似乎也不接受元素的border样式<optgroup>

工作示例: http: //jsbin.com/aquya(可通过http://jsbin.com/aquya/edit编辑)

$(document).ready(function() {
  // Prevent CSS inherits
  $("option").css('backgroundColor', 'white')

  $("option")
    .filter(function(){
      return this.style.display == 'none';
    })
    .parent()
    .not($('option').filter(function(){
      return this.style.display != 'none';
    }).parent())
    .css('backgroundColor', 'blue')
    .css('border', '1px solid red'); //this doesn't work in IE8
});
于 2009-05-08T22:31:22.887 回答
1

// 根据需要回答更改 css 的问题

    if($.browser.msie || $.browser.safari){

        $('optgroup:not(:has(:hidden))').css("border","3px solid red");

    } else {

        $('optgroup:not(:has(:visible))').css("border","3px solid red");

    }

// 删除空的 optgroups 示例

    if($.browser.msie || $.browser.safari){

        $('optgroup:not(:has(:hidden))').remove();

    } else {

        $('optgroup:not(:has(:visible))').remove();

    }
于 2011-04-21T16:04:50.100 回答
0

You'll need to compare an array of all the :visible vs. :hidden

here is some pseudo code

if ($("#element:hidden").length == $("#element:visible").length) {
  // Do  stuff
} ...
于 2009-05-08T19:32:25.890 回答