點擊div實現(xiàn)toggleClass隱藏和出現(xiàn)功能,為什么隱藏后出不來?$(document).ready(function(){ $('#switcher').click(function(event) { if(event.target == this){$('#switcher button').toggleClass('hidden'); }});});<div id="switcher" class="switcher"> <h3>Style Switcher</h3> <button id="switcher-default"> Default </button> <button id="switcher-narrow"> Narrow Column </button> <button id="switcher-large"> Large Print </button> </div>
2 回答

慕田峪4524236
TA貢獻1875條經(jīng)驗 獲得超5個贊
首先別懷疑toggleClass, 而是你用錯了.
其次你為什么要用判斷(event.target == this)??
$(document).ready(function(){
$('#switcher').click(function() {
$('#switcher button').toggleClass('hidden');
});
});
這樣就可以.
另外用toggle()也可以阿.
$(document).ready(function(){
$('#switcher').click(function() {
$('#switcher button').toggle();
});
});
最后 this是JS的對象, $(this)才是jquery對象, $(event.target)也一樣
- 2 回答
- 0 關(guān)注
- 476 瀏覽
添加回答
舉報
0/150
提交
取消