简体   繁体   English

如果陈述适用于javascript中的利润率最高的地方,为什么我不这样

[英]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. 我通过更改元素的margin-top实现此目的。

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: 这是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). $('mobile_menu_container')返回所有<mobile_menu_container>元素(即,很可能什么都没有)。 If you want to get the element with the id mobile_menu_container, you must write: $('#mobile_menu_container') . 如果要获取具有id mobile_menu_container的元素,则必须编写: $('#mobile_menu_container') (Notice the # ). (注意# )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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