其爲每行的工作。如何從任何行一次只使用一個? 僅將類添加到任何行中的單擊列,所有tr類中的所有其他td都應爲空。 請幫助JQuery兄弟姐妹()。只爲每行的點擊列添加類
$('td').click(function(){
$(this).siblings('td').removeClass('active');
$(this).addClass('active');
});
其爲每行的工作。如何從任何行一次只使用一個? 僅將類添加到任何行中的單擊列,所有tr類中的所有其他td都應爲空。 請幫助JQuery兄弟姐妹()。只爲每行的點擊列添加類
$('td').click(function(){
$(this).siblings('td').removeClass('active');
$(this).addClass('active');
});
你需要從所有td
元素的刪除active
類。截至目前,您只能從兄弟td
元素中刪除該類。
$('td').click(function(){
$('td.active').removeClass('active'); //Remove active class from all td
$(this).addClass('active');
});
爲什麼不能簡單地將其設置爲新電池之前刪除所有td
的active
類?
$('td').click(function(){
$('td').removeClass('active');
$(this).addClass('active');
});
.active {
border: solid 1px red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>COL</td>
<td>COL</td>
</tr>
<tr>
<td>COL</td>
<td>COL</td>
</tr>
<tr>
<td>COL</td>
<td>COL</td>
</tr>
<table>
$('#xyz td').click(function(){
$("#xyz").find('td').removeClass('active');
$(this).addClass('active');
});
HTML: -
<table id="xyz">
<tr><td>COL</td><td>COL</td></tr>
<tr><td>COL</td><td>COL</td></tr>
<tr><td>COL</td><td>COL</td></tr>
<table>
CSS: -
.active {border:solid 1px red;}
可以請告訴我這段代碼有什麼問題 – mean
如果你將使用當前td的兄弟,那麼它將只返回一個td,這是它的兄弟姐妹。這是你的代碼不工作的原因。你的代碼中的每一個td都有一個td兄弟姐妹其他td是它的表兄弟不是兄弟 – mean