简体   繁体   English

我不明白如何使用document.getElementById('id')。onclick

[英]I don't understand how to use document.getElementById(‘id’).onclick

I'm learning javascript right now and I am just building a simple menu that will show when the nav button is clicked. 我正在学习javascript,现在正在构建一个简单的菜单,该菜单将在单击导航按钮时显示。

I don't understand how to use the document.getElementById('id').onclick when in a separated js file that is linked to my html. 当我在链接到我的html的单独的js文件中时,我不明白如何使用document.getElementById('id')。onclick。 Reading around I think I understand that my problem is that you cannot call onclick out the blue because the DOM element are not yet defined.. or something alone those line. 环顾四周,我想我理解我的问题是,您无法将onclick称为蓝色,因为DOM元素尚未定义..或仅那些行。 I just don't understand then how to proceed. 我只是不明白该如何进行。

If I add within my button html tag onclick="function()" it works, but it don't when I add it within my separate js file. 如果我在按钮html标签中添加onclick =“ function()”,则可以使用,但是当我在单独的js文件中添加时,则不能使用。 I'm using the W3school tutorial found here . 我正在使用此处找到的W3school教程。

Here is my code 这是我的代码

<nav>
      <button class="nav-button" id="nav">
        <div class="menu-button"></div>
        <div class="menu-button"></div>
        <div class="menu-button"></div>
      </button>
      <div class="dropdown-menu" id="dropdown">
        <a href="#">Hello</a>
        <a href="#">Hello</a>
        <a href="#">Hello</a>
      </div>
    </nav>

.nav-button {
  background: none;
  border: none;
  margin-left: 1em;
  padding-top: 12px;
  cursor: pointer;
}

.menu-button {
  background-color: #fff;
  width: 30px;
  height: 4px;
  margin-bottom: 5px;
  border-radius: 5px;
}

.dropdown-menu {
  margin-top: 7px;
  width: 100px;
  background-color: #fff;
  display: none;
}

.showDropDown {
  display: block;
}


function showDropDown() {
  document.getElementById('dropdown').classList.toggle("showDropDown");
}

document.getElementById("nav").onclick = function showDropDown() 

Here is a codepen 这是一个codepen

Thanks for your help! 谢谢你的帮助!

just instead of 只是代替

document.getElementById("nav").onclick = function showDropDown()

replace it with 替换为

document.getElementById("nav").onclick = showDropDown

because the onclick accepts function and you'r already defined this function 因为onclick接受函数,并且您已经定义了此函数

1.create myjavascript.js file in same folder where your page html is. 1.在页面HTML所在的文件夹中创建myjavascript.js文件。

2.copy the javascript code in myjavascript.js "only code not tag <script></script> " 2.复制myjavascript.js中的javascript代码“仅代码不标记<script></script>

4.paste this at the end of your html page 4.将此内容粘贴到html页面的末尾

<SCRIPT language="javascript" src="myjavascript.js" type="text/javascript"></SCRIPT>

This is the way to reference your code javascript in your html file. 这是在html文件中引用代码javascript的方式。

It will be much easier to work with jquery than plain javascript. 使用jquery比使用纯JavaScript会容易得多。 Here how you are calling showDropDown function is wrong 在这里,您如何调用showDropDown函数是错误的

change function showDropDown to showDropDown change function showDropDownshowDropDown

Better you can make the closure at the button click event like 更好的是,您可以在按钮单击事件中进行关闭,例如

let btn = document.getElementById("nav"); 
let toggleIt = document.getElementById('dropdown'); 
btn.onclick = function(){  
toggleIt.classList.toggle("showDropDown"); 
};

Having it in a separate js file should work you just need to import the js file using the script tag. 将其保存在单独的js文件中应该可以正常工作,您只需要使用script标签导入js文件即可。 You should probably do this at the end of the HTML to guarantee that the js is ran after the DOM is loaded. 您可能应该在HTML末尾执行此操作,以确保在加载DOM之后运行js。

Update Your missing the brackets around the function call: 更新您缺少函数调用方括号:

document.getElementById("nav").onclick = function() { showDropDown(); document.getElementById(“ nav”)。onclick = function(){showDropDown(); }; };

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

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