2012-08-15 105 views
5

您可以在jsfiddle上看到http://jsfiddle.net/C8B8g/7/我有2個按鈕(= 2個div)「區域」和「源」。每個div在鼠標上被突出顯示(藍色背景)(感謝stackoverflow貢獻者導致我仍然非常糟糕的JS)。單擊其他DIV時更改DIV顏色

我注意到用戶實際上並沒有意識到這些是按鈕,所以我想要做的是讓第一個包含文本「我們的區域」的DIV在默認情況下用藍色背景突出顯示當包含文字「我們的來源」的其他div被點擊時(在這種情況下,「我們的來源」背景變成藍色),回到白色背景。做了一些測試在CSS,但沒有成功, 「當前」 用...

回答

6

試試這個: 更新時間: Here is working jsFiddle.

$('.activity-title a:first').css({'background-color':'#f00','color':'#fff'}); 
//for setting first button highlighted by default, 
//don't forgot to define document.ready before this 

$('.activity-title a').click(function() { 
    var region = $(this).attr('data-region'); 

    $('.activity-title a').css({'background-color':'#fff','color':'#467FD9'}); 
    $(this).css({'background-color':'#f00','color':'#fff'}); 

    $('.textzone:visible').stop().fadeOut(500, function() { 
    //don't forgot to add stop() for preventing repeat click conflict 
     $('#' + region).fadeIn(500); 
    }); 

    return false; 

});​ 
+0

請寫出這是爲什麼downvoted改進的答案。 – 2012-08-15 09:11:31

+0

謝謝 - 它工程太棒了!有沒有辦法讓第一個按鈕默認突出顯示? (因爲右側的文本區域是可見的,這將幫助人們瞭解每個按鈕都與文本區域相關聯) – Greg 2012-08-15 09:15:39

+0

@Greg i更新了我的答案配對,您可以將這些css值分配給類或id,並使用removeClass/addClass方法,但你問如何改變與CSS的CSS,所以我回答了這種方式。 – 2012-08-15 10:02:22

1
try with this exemple: 
<div id="target"> 
Test1 
</div> 
<div id="other"> 
    Test2 
</div> 

<script> 
$("#target").click(function() { 
    $('#target').css({'background':'blue'}); 
$('#other').css({'background':'white'}); 
}); 

$("#other").click(function() { 
    $('#other').css({'background':'blue'}); 
    $('#target').css({'background':'white'}); 
}); 
</script> 
5

添加樣式選擇DIV

.source-selected { 
    background-color:#00F; 
} 

添加這個類到你的默認div。

添加此行到您的單擊處理程序(更新)

if(!$(this).closest('.source-title-box').hasClass('source-selected')) 
{ 
    $('.source-title-box').toggleClass('source-selected'); 
} 

嘗試更新小提琴:

http://jsfiddle.net/dmvcQ/1

+1

我更喜歡你的方法,因爲它避免了內聯的CSS。然而,這將交替使用該類,它不會影響您點擊哪個鏈接。您需要在另一條線路添加的一個,這樣的事情之前添加: '$ toggleClass(「源選擇‘);' – Jeemusu 2012-08-15 09:06:48

+0

你可能還需要檢查的(’源選擇。」)。最接近的('。source-title-box'),因爲你的代碼選擇了它們全部。 – Jeemusu 2012-08-15 09:12:00

+0

你是對的,我試圖變快;)我已經修復了小提琴和根據你的意見更新了答案。謝謝。 – 2012-08-15 09:17:04

3

好吧,我決定發佈一個答案,因爲有你的代碼的某些部分我想要解決。

首先,不要在最後做一個return false;,而可以使用jQuery event.preventDefault();函數。最終的結果基本上是相同的,但是通過這種方式,我們可以使用jQuery在開始運行其他代碼之前告訴錨點不做任何事情。

我更新的Javascript代碼:

$('.source-title-box a').click(function(event) { 

    // Stop the default anchor behaviour 
    event.preventDefault(); 

    // Lets check if the clicked link is already active 
    // And if it is active, don't let them click it! 
    if($(this).closest('div').hasClass('active')) { 
     return false; 
    } 

    // Now lets remove any active classes that exist 
    $('.active').toggleClass('active'); 

    // And apply an active class to the clicked buttons 
    // parent div 
    $(this).closest('div').toggleClass('active'); 

    var region = $(this).attr('data-region'); 

    $('.textzone:visible').fadeOut(500, function() { 
     $('#' + region).fadeIn(500); 
    }); 
});​ 

我添加的CSS:

.active { 
    background-color:#467FD9; 
    color:#fff; 

} 
.active a { 
    cursor:default; /* Don't show the hand cursor */ 
    color:#fff; 
} 

工作示例:

http://jsfiddle.net/dmvcQ/3/

我不知道如果當前的HTML標記有其他目的,但目前的風格和功能ty只需要<a href="#" data-region="source-region">Our region</a>即可實現,您無需將它們包裝在<div><span>中。

例如,下面是相同的代碼,只用錨標籤:http://jsfiddle.net/dmvcQ/7/

讓我知道如果您對回答任何問題。

+1

謝謝你的回答Jeemusu!出於某種原因,在實時環境中(點擊頁面上的「源代碼」http://goo.gl/fOHGN),第一個'按鈕'不會像jsfiddle一樣默認突出顯示。你知道爲什麼嗎?謝謝 – Greg 2012-08-15 11:03:20

+1

您是否將活動類添加到默認顯示爲選定的html元素。檢查我的Js小提琴,看看我是如何設置它的。 – Jeemusu 2012-08-15 13:35:09

+0

哎呀,我錯過了那個 - 現在沒事,非常感謝您的幫助! – Greg 2012-08-15 17:29:52

1

或者你也可以做到這一點,主要的jQuery

工作的示例:

http://jsfiddle.net/razmig/GhbjS/

$('.activity-title a:first').css({ 
    "background-color": "#467FD9", 
    "color": "#fff" 
}); 


$('.activity-title a').mouseover(function() { 
    $('.activity-title a').css({ 
     "background-color": "#fff", 
     "color": "#467FD9" 
    }); 
    $(this).css({ 
     "background-color": "#467FD9", 
     "color": "#fff" 
    }); 

}); 

$('.activity-title a').click(function() { 
    var region = $(this).attr('data-region'); 

    $('.textzone:visible').fadeOut(2000, function() { 
     $('#' + region).fadeIn(2000); 
    }); 

    return false; 

}); 
+0

感謝您的回答和工作示例。我注意到你改變了href =「#」的html。不幸的是,在我的情況下(一個頁面佈局,每節1格)不起作用(單擊按鈕使頁面滾動到頂部) – Greg 2012-08-15 11:05:50

+1

只需添加href =「javascript:void(0)」或在jquery中可以使用event.preventDefault(); – Razmig 2012-08-16 05:48:45