我有一些簡單的標記畫一個下拉菜單和不同複雜程度的幾個div的:jQuery的:隱藏多深的所有div,然後顯示一個
<select class='sel'>
<option data-opt='a'>show only 'a' boxes</option>
<option data-opt='b'>show only 'b' boxes</option>
<option data-opt='*'>show all</option>
</select>
<div class='holder'>
<div class='a'>
<div>
<p>Here is some text (A)</p>
</div>
<p>Plus more stuff in here.</p>
</div>
<div class='a'>
<p>More text (A)</p>
</div>
<div class='b'>
<p>Here is other text (B)</p>
<div><span>There is more in here!</span></div>
</div>
<div class='b'>
<p>Even more text (B)</p>
</div>
</div>
而當用戶選擇從一個選項下拉,我想隱藏不匹配的DIV,並且只顯示匹配的DIV:
$('.sel').change(function() {
opt = $(this).find(":selected").data('opt');
console.log('option chosen: '+opt);
if(opt == '*') { // select all
console.log('show all');
$('.holder').show();
} else { // hide all but this
$('.holder :not(div'+opt+')').hide();
$('.holder').find('div'+opt).show();
}
});
但是,由於某種原因,它不工作。它看起來像hide()方法隱藏每個元素(包括主DIV的子/兄弟),然後show()方法只顯示初始DIV。而顯示全部選項根本不起作用。所以有一些深度問題。我怎樣才能解決這個問題?
的jsfiddle:http://jsfiddle.net/pnoeric/FjEBY/3/
不要忘了挑你用什麼:) –