简体   繁体   中英

Why isn't my else if statment working for margin-top in javascript

My function works for making the menu drop down from the top but I want to be able to click it and make it go back up again. I'm achieving this with changing the margin-top of the element.

Here's the function:

    <!--Mobile Menu On Click-->
    <script type="text/javascript">
    function mobile_menu(){
    if($('mobile_menu_container').css('margin-top') !=== '0px'){
    document.getElementById('mobile_menu_container').style.marginTop="0px";
    } 

    else if($('mobile_menu_container').css('margin-top') === '0px'){
    document.getElementById('mobile_menu_container').style.marginTop="-260px";
    } 
    }
    </script>

Here's the html:

<a href="#" onClick="mobile_menu()"> 
<p id='tap_to_view'>
Tap To View Menu<br />
</p>
</a>

$('mobile_menu_container') returns all <mobile_menu_container> elements (that is, most likely, nothing). If you want to get the element with the id mobile_menu_container, you must write: $('#mobile_menu_container') . (Notice the # ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM