1.問題描述
假設(shè)有這么一段代碼康二,我該如何獲得指定的div,并對其操作沫勿?
<div class="xinhao_category"></div>
<div class="xinhao_category"></div>
<div class="xinhao_category"></div>
<div class="xinhao_category"></div>
<div class="xinhao_category"></div>
2.解決方案
通過$(this).index()獲取對應(yīng)元素的下標(biāo)浅蚪;
通過siblings()方法可以實現(xiàn)?同級元素?的反選功能;
// 設(shè)置點擊事件
$('.xinhao_category').on('click', function() {
? ? ? $('.xinhao_category:eq(' + $(this).index() + ')').css({'background': 'orange', 'color': 'black'});
? ? ? $('.xinhao_category:eq(' + $(this).index() + ')').siblings().css({'background': 'rgba(255, 255, 255, 0.1)', 'color': 'white'});
});