我有兩個類似的HTML塊(一個用於新聞,第二個用於特殊優惠),它們在底部都有分頁按鈕,所以我嘗試構建一個通用函數來更改分頁按鈕狀態(活動/不活動)爲每個塊獨立,但我失敗了。這就是問題所在。我該怎麼做?使用jQuery獨立更改兩個類似元素的CSS類
這裏是的jsfiddle:http://jsfiddle.net/iamsvyat/5TjKQ/
HTML:
<div id="news-block">News Block</div>
<div class="pag-circles">
<button class="pag-circle-1 active"> </button>
<button class="pag-circle-2 inactive"> </button>
<button class="pag-circle-3 inactive"> </button>
<button class="pag-circle-4 inactive"> </button>
</div>
<div id="special-offers-block">Special Offers Block</div>
<div class="pag-circles">
<button class="pag-circle-1 active"> </button>
<button class="pag-circle-2 inactive"> </button>
<button class="pag-circle-3 inactive"> </button>
<button class="pag-circle-4 inactive"> </button>
</div>
CSS:
button {
width: 0;
height: 0;
margin: 0;
padding: 0;
border: 6px solid black;
border-radius: 6px;
}
.active {
border-color: red;
}
.inactive {
border-color: black;
}
button:focus {
outline: none;
}
button:active {
position: relative;
top: 1px;
}
jQuery的:使用事件德勒
$("#news-block").next(".pag-circles").children().click(function() {
//if the first button is clicked
//if the second button is clicked
//if the third button is clicked
//if the fourth button is clicked
});
$("#special-offers-block").next(".pag-circles").children().click(function() {
//if the first button is clicked
//if the second button is clicked
//if the third button is clicked
//if the fourth button is clicked
});
將按鈕設置爲同一類是否合理? – j08691