2014-10-03 901 views
1

的身體的CSS樣式我有一個sample here這裏我試圖切換使用jQuery的CSS樣式身體,切換一個HTML頁面

$('body').toggle(function() { 
     (this).css('padding-top', '0px'); 
     (this).css('background-color', 'green'); 
    }, 
     function() { 
     (this).css('padding-top', '50px'); 
     (this).css('background-color', 'yellow'); 
     }); 

但是當我點擊按鈕的身體完全消失。我怎樣才能達到相同的目標?

回答

3

嘗試使用toggleClass代替

http://jsfiddle.net/nwL67ao4/1/

CSS:

body { 
    padding-top: 50px; 
    background-color: yellow; 
} 
.hide { 
    padding-top: 0px; 
    background-color: green;  
} 

JS:

$('button').click(function() {  
    $('body').toggleClass("hide"); 
}); 

HTML:

<button>ABCD</button> 
+1

這是個好主意+1 – 2014-10-03 11:15:37

+1

只需要注意:'.toggle()'在1.8版本中被棄用,從版本1.9中刪除了 – GibboK 2014-10-03 11:17:12

+1

@GibboK我很高興你提到'.toggle()'事件處理程序'.toggle()'用於切換顯示/隱藏)已棄用,+1。 – Regent 2014-10-03 11:19:55