简体   繁体   English

如何更改此元素的样式属性?

[英]How can I change the style properties of this element?

I'm trying to make a drop down menu for the mobile version of a website and can't seem to figure out what I'm doing wrong. 我正在尝试为网站的移动版本创建一个下拉菜单,但似乎无法弄清我做错了什么。 I have the initial position where I want it (fixed in the upper right corner of the screen) but it does nothing when you click on the Menu tab. 我有想要的初始位置(固定在屏幕的右上角),但是当您单击“菜单”选项卡时,它什么也没做。 Here's my JSFiddle 这是我的JSFiddle

<div id="mobile-nav-wrapper">
    <ul id="mobile-nav">
        <li>Main Menu</li>
        <ul>
            <li>Home</li>
            <li>About</li>
            <li>Contact</li>
            <li>Register</li>
            <li>FAQs</li>
            <li>Facebook</li>
        </ul>
        <li>This Page</li>
        <ul>
            <li>Some Text</li>
            <li>Who?</li>
        </ul>
    </ul>
    <span id="mobile-nav-button" onclick="pullmenu('#mobile-nav-wrapper')">Menu</span
</div>

Your JavaScript function isn't a global. 您的JavaScript函数不是全局函数。 You have configured JSFiddle to wrap it in an onload handler. 您已经配置了JSFiddle以将其包装在onload处理程序中。 It isn't accessible outside the scope of that function. 在该功能范围之外无法访问。

Don't use intrinsic event attributes, they depend on globals. 不要使用内部事件属性,它们取决于全局变量。 Bind your event handlers with JS instead. 而是将事件处理程序与JS绑定

span isn't an interactive element. span不是交互式元素。 It doesn't appear in the focus order so has serious accessibility implications when you depend on people interacting with it. 它没有出现在焦点顺序中,因此当您依赖与之交互的人时,它会严重影响可访问性。 Use a button instead. 请改用按钮。 Apply CSS if you don't like the way it looks. 如果您不喜欢CSS的外观,请应用CSS。

<button type="button" id="mobile-nav-button">Menu</button>

<script>
document.getElementById('mobile-nav-button').addEventListener('click', function (evt) {
   pullmenu('#mobile-nav-wrapper');
});

function pullmenu(etc) { etc }
</script>

JavaScript 的JavaScript

<script>
function pullmenu(menu) {
var x = document.getElementById(menu).style;
if (x.top == "-15em") {x.top="0";}
else {x.top="-15em";}
}
</script>

HTML : HTML

<span id="mobile-nav-button" onclick="pullmenu('mobile-nav-wrapper')">Menu</span>

Example

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

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