简体   繁体   English

JavaScript参考html5语义标记

[英]JavaScript reference html5 semantic tag

I'm new to JavaScript and this site. 我是JavaScript和这个网站的新手。 I need help with trying to toggle a div's visibility, the div is a html semantic tag ( nav ), and I can't find a way to reference it. 我在尝试切换div的可见性时需要帮助,该div是html语义标记( nav ),但是我找不到引用它的方法。

I have searched for a good hour and have tried document.getElementById() , but that doesn't work unless I change the tag to <div id="nav"> . 我已经搜索了一个好时机并尝试了document.getElementById() ,但是除非将标记更改为<div id="nav">否则该方法将无法正常工作。

My JavaScript code is as follows: 我的JavaScript代码如下:

function toggle_visibility(id) {
"use strict";

var e = document.getElementById(id);

if (e.style.display === 'block') {

    e.style.display = 'none';
} else {

    e.style.display = 'block';
}}

And I'm calling it from another div in my html like this 我从这样的html中的另一个div调用它

<a href="#" onclick="toggle_visibility('nav');"><div id="mNav"></div></a>

<nav>
    <ul>
        <a href=""><li>
            Link 1
        </li></a>
        <a href=""><li>
            Link 2
        </li></a>
        <a href=""><li>
            Link 3
        </li></a>
    </ul>
</nav>

In your case document.getElementById is used only if the element has an ID. 在您的情况下,仅当元素具有ID时才使用document.getElementById But if you want to get reference of the element using tag name, then you must use document.getElementsByTagName . 但是,如果要使用标签名获取元素的引用,则必须使用document.getElementsByTagName But again this would make your generic function toggle_visibility to be limited to tagname. 但这又会使您的通用函数toggle_visibility限于标记名。

Instead of both, use document.querySelector . 不要使用二者,而是使用document.querySelector To this function, you can pass css selectors and it can be either id or tag name or class name as well. 对于此功能,您可以传递css选择器,它也可以是id或标记名或类名。

 function toggle_visibility(selector) { "use strict"; var e = document.querySelector(selector); e.style.display = (e.style.display==="block") ? "none" : "block"; } 
 <a onclick="toggle_visibility('nav');">Toggle Menu<div id="mNav"></div></a> <nav> <ul> <a href=""><li> Link 1 </li></a> <a href=""><li> Link 2 </li></a> <a href=""><li> Link 3 </li></a> </ul> </nav> 

Also instead of binding the click event to the element inside the HTML, use addEventListener to bind the event. 另外,不要将click事件绑定到HTML内的元素,而是使用addEventListener绑定事件。 It would be easier for you to manage. 您可以更轻松地进行管理。

Now in your HTML you can even reference like: 现在,您甚至可以在HTML中进行引用,例如:

toggle_visibility("#myNav")
toggle_visibility(".className")

nav is not id in your case, but a tag name. nav在您的情况下不是id,而是标签名。 In this case, use: 在这种情况下,请使用:

var e = document.getElementsByTagName(id)[0];

Or, more flexible solution: 或者,更灵活的解决方案:

var e = document.querySelector(id);

(also, as it's not id, you probably want to replace id parameter with tag , but that's just cosmetic :) ) (此外,因为它不是id,所以您可能想用tag替换id参数,但这只是装饰性的:)

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

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