2010-03-18 53 views
0

從昨天開始跟進...如何過濾所選對象,然後隱藏未選中的對象

這部分代碼確實有效。

$(document).ready(function(){ 
$('#listMenu a').click(function (selected) { 
     var getPage = $(this).attr('id'); 
     var getName = $(this).attr('name'); 

     //console.log(getPage); 
     //console.log(getName); 

      $(function() { 
      $("#" + getName).show(); 
      var getColor = $('#' + getName).css('background-color'); 
      $('#header').css('background', getColor); 
      $('#slideshow').css('background', getColor); 
      $('span').css('background', getColor); 
      $('#menuContainer').css('background', getColor); 

     }); 

     $('li.listMenu ul').hide(); 
    });   
}); 

我能夠得到我想要的東西;基於vars getPage和 getName,現在我需要隱藏未選中的內容。

我已經試過這樣:

$(function() { 
    var notSelected = $('div').filter(selected).attr('id', 'name'); 
    $(this).hide(); 
    console.log(notSelected); 
}); 

放在略高於:$('li.listMenu ul').hide();

但它是不正確的,記得我是一個真正的新手。

我在屏幕上看到的是應該隱藏的新選定項目。

再次讚賞任何幫助。

-sjs

回答

2

我相信你想這個,如果你點擊一個<a>一個DIV中:

$('div').not($(this).closest('div')).hide(); 

或者,如果你打算淡出其他<a>元素:

$('#listMenu a').not(this).hide(); 

.not() function過濾那些匹配出來的元素,所以.hide()將運行在其餘的o f元素。在你的情況下,selected是點擊事件,你想要的是this,它指的是點擊來自的#listMenu a