我在嘗試一些非常簡單的事情。在頁面加載時,內容div中的數據被dsplayed並且菜單(ul元素)被隱藏。點擊通過css創建的菜單按鈕後,菜單加載並且內容div被隱藏。當我再次點擊菜單按鈕時,它使用toggleClass()隱藏菜單。在這一點上它的目的是顯示在內容股市的數據,但這不是按預期工作。jQuery的顯示div toggleClass
希望你能告訴我我做錯了什麼。
android.js:
// JavaScript Document
if (window.innerWidth && window.innerWidth <= 480){
//when the document is ready run this code. wait for the page to load completely. otherwise javascript will run and because the uls don't exist
//yet it will fail. because the js has failed the uls will load (not good)
$(document).ready(function() {
$('#header ul').addClass('hide'); //hide ul elements which is the menu on page load (see android.css #header ul.hide..)
$('#header').append('<div class="leftButton" onclick="toggleMenu()">Menu</div>');
});
//currently the menu (ul) elements are hidden as set on page load
//toggleClass() works by checking what class its set to. if its already set to that class it changes it to the other.
function toggleMenu(){
if($('#header ul').toggleClass('hide')){//adding hide class to the object
$('#content').show();
}
if ($('#header .leftButton').toggleClass('pressed')){
$('#content').hide();
}
}
}
CSS文件:
#header ul.hide{
display:none;
}
#header div.leftButton{
position:absolute;
top:8.5px;
left:6px;
height:30px; /*set hight to 30px so its big enought to tap*/
font-weight:bold;
text-align:center;
color:white;
text-shadow:rgba(0,0,0,0.6) 0px -1px 1px;
line-height:28px; /*centers it within the div so its not up against the top border*/
/*placing the button*/
border-width:0 8px 0 8px;
-webkit-border-image:url(images/button.png) 0 8 0 8;
}
#header div.pressed{
position:absolute;
top:8.5px;
left:6px;
height:30px; /*set hight to 30px so its big enought to tap*/
font-weight:bold;
text-align:center;
color:white;
text-shadow:rgba(0,0,0,0.6) 0px -1px 1px;
line-height:28px; /*centers it within the div so its not up against the top border*/
/*placing the button*/
border-width:0 8px 0 8px;
-webkit-border-image:url(images/button_clicked.png) 0 8 0 8;
}
#content.hide{
display:none;
}
希望以上足以識別爲什麼它不工作。謝謝!
的['.toggleClass()'方法](http://api.jquery.com/toggleclass/)返回它被調用的同一個jQuery對象,_not_是一個布爾值,所以將它用作'if'條件沒有任何意義 - 條件將始終爲真因爲任何對象都是「真理」。因此'.show()'然後是'.hide()'每次都會運行... – nnnnnn