2013-03-14 16 views
0

人民事業部獲得色彩和點擊失去與jQuery

在這裏我有一個等級(缺點,平均優勢)和按鈕JS代碼,所有這些參數。

但我有一個問題,當我點擊劣勢,平均和優勢按鈕,它必須失去它的顏色,如果之前點擊這些按鈕,這也是一樣的,如果我點擊平均或優勢,因爲其他按鈕必須失去他們的顏色,只有點擊按鈕才能獲得顏色。問題在哪裏。我把代碼中的一個的jsfiddle:

http://jsfiddle.net/mwsvP/

$(".ca_button, .ca_button1").click(function() { 

    $this = $(this); 

    if ($this.is("a")) { 
     $this = $this.parent(); 
    } 

    $par = $this.parent(); 

    $par.find(".ca_button, .ca_button1").css("background-color", "#bababa"); 

    if ($(this).hasClass("ca_button1")) { 
     $this.find("a").css("background-color", "#0F0"); 
    } else { 
     $this.find("a").css("background-color", "#F00"); 
    } 

    return false; 

}); 
+0

http://jsfiddle.net/mwsvP/1/ – 2013-03-14 14:26:23

回答

0

我認爲問題出在你定義這個變量的方式:

$this = $(this); 

應該更好地工作,如果你這樣做:

var t = $(this); 

然後用t到處都是你現在用的$this

$(".ca_button, .ca_button1").click(function() { 

    var t = $(this); 

    if (t.is("a")) { 
     t = t.parent(); 
    } 

    $par = t.parent(); 

    $par.find(".ca_button, .ca_button1").css("background-color", "#bababa"); 

    if ($(this).hasClass("ca_button1")) { 
     t.find("a").css("background-color", "#0F0"); 
    } else { 
     t.find("a").css("background-color", "#F00"); 
    } 

    return false; 

}); 

這裏是你更新的jsfiddle:http://jsfiddle.net/mwsvP/2/

+0

請的jsfiddle解釋更好地瞭解您think.THANKS什麼! – 2013-03-14 14:27:31

+0

添加了更新的jsfiddle。 – Bazzz 2013-03-14 14:29:10

+0

http://jsfiddle.net/mwsvP/1/ – 2013-03-14 14:30:20