我所擁有的是同一類3個div,但有一個唯一的id,當我單擊一個框時,我想隱藏另一個2.如何使用jQuery定位未被點擊的同一類的項目?
這不是問題,我可以通過幾個如果/ elses或case語句可能,但我想知道是否有更通用/有效的方法來隱藏同一類的所有元素,而不是被點擊的元素?
<div id="boxes">
<div id="box1" class="box">Box 1</div>
<div id="box2" class="box">Box 2</div>
<div id="box3" class="box">Box 3</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.box').click(function() {
$(this).html('hi');
if ($(this).attr('id') == 'box1')
{
$('#box2').hide('slow');
$('#box3').hide('slow');
}
.......... more if's
});
});
</script>
,如果你使用像$處理程序不要你這是點擊的元素(「盒子」)點擊(函數(element){...}? –
是的,我可以得到被點擊的元素,但是一旦點擊了這個元素,我想隱藏其他2個盒子,只是尋找更有效的方法而不是使用3個條件語句。 – martincarlin87