2014-11-04 137 views
0

需要一些幫助。管理創建一個jQuery來改變被點擊後的按鈕的類,但我需要你點擊一個新的按鈕類被從其他人中刪除。點擊新按鈕後刪除課程

我在嘗試,但我不能。我嘗試了一些方法,但每當我嘗試時,我都可以刪除所有內容,然後單擊與新類別不同的​​按鈕。

任何人都可以幫助我嗎?

$(document).ready(function() { 
 

 
    $('div#filters-container button').click(function() { 
 

 
    var id_button = $(this).val(); 
 
    $("#" + id_button + "").addClass("cbp-filter-item-active"); 
 

 
    }); 
 

 
});
.cbp-filter-item-active { 
 
    background-color: #5d5d5d; 
 
    color: #fff!important; 
 
    border-color: #5d5d5d 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="filters-container"> 
 
    <button value="0" id="0" href="videos2-carrega.asp">Todos</button> 
 
    <button value="1" id="1" href="videos2-carrega.asp?cat=1">Família</button> 
 
    <button value="2" id="2" href="videos2-carrega.asp?cat=2">Finanças</button> 
 
    <button value="3" id="3" href="videos2-carrega.asp?cat=3">Propaganda</button> 
 
    <button value="4" id="4" href="videos2-carrega.asp?cat=4">Empreendedorismo</button> 
 
</div>

感謝您的關注。

+0

只顯示你寫的代碼,而不是小提琴鏈接 – 2014-11-04 11:24:43

+1

代碼pleeeeazzz。您將無法添加鏈接到jsfiddle,直到您在那裏顯示代碼。 – nicael 2014-11-04 11:25:00

+0

請輸入密碼+小提琴。 – sinisake 2014-11-04 11:25:35

回答

2
​​

添加的代碼行將刪除其他按鈕的類。 DEMO:http://jsfiddle.net/8oytw0ue/2/

其實,簡單得多:

$(document).ready(function() { 

    $('div#filters-container button').click(function() { 

    $(this).addClass("cbp-filter-item-active"); 
     $(this).siblings().removeClass('cbp-filter-item-active'); 

    }); 

}); 

你不需要ID選擇按鈕$(這)將是確定...

+0

謝謝Evermind !!!!!!!! ! – 2014-11-04 11:31:06

+1

@Ed他是Nevermind:D – nicael 2014-11-04 11:32:07

0

$(document).ready(function() { 
 

 
    var $buttons = $('div#filters-container button').click(function() { 
 

 
    var $this = $(this).addClass("cbp-filter-item-active"); 
 
    $buttons.not($this).removeClass("cbp-filter-item-active"); 
 

 
    }); 
 

 
});
.cbp-filter-item-active { 
 
    background-color: #5d5d5d; 
 
    color: #fff!important; 
 
    border-color: #5d5d5d 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="filters-container"> 
 
    <button value="0" id="0" href="videos2-carrega.asp">Todos</button> 
 
    <button value="1" id="1" href="videos2-carrega.asp?cat=1">Família</button> 
 
    <button value="2" id="2" href="videos2-carrega.asp?cat=2">Finanças</button> 
 
    <button value="3" id="3" href="videos2-carrega.asp?cat=3">Propaganda</button> 
 
    <button value="4" id="4" href="videos2-carrega.asp?cat=4">Empreendedorismo</button> 
 
</div>

0

您可以將類添加到當前單擊的元素並從同級元素中刪除它們:

$('div#filters-container button').click(function() { 
    $(this).addClass("cbp-filter-item-active").siblings().removeClass('cbp-filter-item-active'); 
}); 

Working Demo

0

您可以使用此:http://jsfiddle.net/620tqr19/

$('button').on('click',function(){ 
    $('button').removeClass('active'); 
    $(this).addClass('active'); 
}); 

更改類= 「活動的」 按要求。