2013-05-01 94 views
0

我有jQuery的一點小毛病而被jQuery的變化CSS有點與它新手這裏是我的問題:div嵌套

我想提出一個下拉麪板菜單,我已經得到了下拉OK但需要影響div內嵌入div的背景圖像,div被點擊觸發動作。即「.side_bar_top_text」是需要改變背景的div。

的jQuery:

<script type="text/javascript"> 
$(document).ready(function() { 
$('.side_bar_top').click(function(){ 
    if ($(this).attr('class') != 'active'){ 
     $('.side_bar .side_bar_panel', this).slideUp(); 
     $(this).next().slideToggle(); 
     $(this).removeClass('active'); 
     $(this).addClass('active'); 

    } 
    }); 
}); 
</script> 

HTML:

<div class="side_bar_top"><div class="side_bar_top_text">COMMERCIAL CONSTRUCTION</div></div> 
<div class="side_bar"> 
    <div class="side_bar_panel"> 
    /*the list of elements*/ 
    </div> 
</div> 

所有我需要做的是通過CSS改變side_bar_top_text的背景圖像時,面板向上,反之亦然。 我試過各種方法,沒有運氣。 任何人都可以幫忙嗎?

+2

'$('。side_bar .side_bar_panel',this)'永遠不會工作。當你在'side_bar_top'內搜索這些元素,並在'side_bar'之前關閉那個元素時,它應該是'$(this).closest('。side_bar')。find('。side_bar_panel')' – Ohgodwhy 2013-05-01 16:19:13

+0

There是jQuery [這裏](http://api.jquery.com/css/)中的一個css方法調用。你已經嘗試了什麼? – 2013-05-01 16:19:18

回答

0

會這樣的事情能幫助你嗎? http://jsfiddle.net/jgQx3/2/

CSS:

.side_bar_top { 
    background: url(path/file1.jpg); 
} 

.side_bar_top.active { 
    background: url(path/file2.jpg); 
} 

SCRIPT:

$(document).ready(function() { 
    $('.side_bar_top').on('click', function(){ 
    var $this = $(this); 

    $this.toggleClass('active'); 
    $this.next('.side_bar').slideToggle(); 
    }); 
}); 

HTML:

<div class="side_bar_top active"> 
    <div class="side_bar_top_text">COMMERCIAL CONSTRUCTION</div> 
</div> 
<div class="side_bar"> 
    <div class="side_bar_panel"> 
    /*the list of elements*/ 
    </div> 
</div> 

此功能已經不垮打開選項雖然。

+0

哈哈你是個天才,謝謝!! – john 2013-05-01 17:24:50

0

爲了獲得元素side_bar_top_text這是點擊的元素的一個孩子,這樣做:

$(this).find('side_bar_top_text'); 

要更改背景:

$(this).find('side_bar_top_text').css('background-image', '<your_image>'); 
0

在CSS:

.side_bar_top .side_bar_top_text{ 
    background:url(path-to-original-image.jpg); //whatever you want 
} 

.side_bar_top.active .side_bar_top_text{ 
    background:url(path-to-image.jpg); //whatever you want 
} 

但如果你有一個特定的圖像爲每個或其他原因在jQuery中做到這一點:

$(document).ready(function() { 
$('.side_bar_top').click(function(){ 
    $(this).siblings().removeClass('active'); 

    if ($(this).attr('class') != 'active'){ 
     $('.side_bar .side_bar_panel', this).slideUp(); 
     $(this).next().slideToggle(); 
     $('.side_bar .side_bar_panel', this).removeClass('active'); 
     $(this).addClass('active'); 

     $(this).find('.side_bar_top_text').css('background', 'url(path-to-image.jpg)'); 

    } 
    }); 
}); 

編輯:

在CSS中,你可以看到,我們覆蓋的活動類的背景。原始圖像在上面定義。 該方法需要jquery添加和刪除點擊活動類。

我加入這行到你的聽衆:

$(this).siblings().removeClass('active'); 

應主動刪除了點擊的元素的每一個兄弟。這應該會恢復您的原始圖像。

+0

很好。我有它改變圖像與CSS(jQuery將無法正常工作這jQuery方法是我已經嘗試過),但與CSS方法我怎麼得到它改變圖像回到谷歌了? – john 2013-05-01 16:39:22

+0

我編輯了我的答案。 jQuery將在這方面工作,但如果它沒有做到你想做的事情,那麼這可能是你編寫監聽器的一個問題。檢查您的控制檯以查看是否有錯誤。 – 2013-05-01 16:47:25

+0

看起來類活動不會被刪除。我編輯了我原來的帖子,以顯示我現在使用的代碼。有任何想法嗎? – john 2013-05-01 17:08:18