简体   繁体   English

如何转换此代码以便函数可以采用变量?

[英]How do I convert this code so the functions can take a variable?

I am trying to change the color of nav bar elements when the mouse goes over them.当鼠标经过它们时,我正在尝试更改导航栏元素的颜色。

This piece of code does that but for only the first button:这段代码可以做到这一点,但仅适用于第一个按钮:

let navOne = document.getElementById("nav1");

function mouseOn() {
    nav1.style.color = "red";
}

function mouseOff() {
    nav1.style.color = "black";
}

navOne.addEventListener('mouseover', mouseOn);
navOne.addEventListener('mouseout', mouseOff);

I have been trying to convert the code so the functions work for multiple buttons, but cannot seem to get it to work.我一直在尝试转换代码,以便这些功能适用于多个按钮,但似乎无法使其正常工作。 Here is the code so far:这是到目前为止的代码:

let navOne = document.getElementById("nav1");

function mouseOn(navButton) {
    navButton.style.color = "red";
}

function mouseOff(navButton) {
    navButton.style.color = "black";
}

navOne.addEventListener('mouseover', mouseOn(navOne));
navOne.addEventListener('mouseout', mouseOff(navOne));

It has no errors, but does not cause any color change when I move my mouse button over the nav1 element.它没有错误,但当我将鼠标按钮移到 nav1 元素上时不会导致任何颜色变化。

Any help is greatly appreciated.任何帮助是极大的赞赏。

You can have the event handler functions take advantage of the event parameter that is passed in:您可以让事件处理函数利用传入的event参数:

 function mouseOn(e) { e.target.style.color = "red"; } function mouseOff(e) { e.target.style.color = "black"; } for (let navItem of document.querySelectorAll('nav a')) { navItem.addEventListener('mouseover', mouseOn); navItem.addEventListener('mouseout', mouseOff); }
 nav a { display: inline-block; padding: 10px; color: black; }
 <nav> <a href="#" id="nav1">Nav One</a> <a href="#" id="nav2">Nav Two</a> </nav>

However, using a css :hover pseudo-attribute would be the preferred approach nowadays.但是,现在使用 css :hover伪属性将是首选方法。 No JS code needed.无需 JS 代码。

 nav a { display: inline-block; padding: 10px; color: black; } nav a:hover { color: red; }
 <nav> <a href="#">Nav One</a> <a href="#">Nav Two</a> </nav>

暂无
暂无

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

相关问题 如何将jQuery代码转换为函数? - how can convert jquery code to functions? 如何将多个嵌套函数导出到其他文件,以便我可以重用代码 - how to exports multiple nested functions to the other files so i can reuse the code 如何获取用户输入并将字符串转换为可以在函数中数学使用的整数? - How do i take user input and convert the string into an integer that i can use mathematically in a function? 我该怎么做才能使函数不堆叠? - What can I do so functions don't stack? 如何过滤文本,以便将部分文本转换为货币? - How do I filter text so I can convert part of it to a currency? 如何将创建的 div 元素转换为对象,以便可以转换为 JSON? - How do I turn created div Element into an object so that I can convert to JSON? 如何将文件 object 转换为填充的文件输入,以便我可以通过表单提交? - How do I convert a file object to a populated file input so I can submit via form? 如何将JSON序列化的对象转换为JS对象,以便可以访问其属性? - How do I convert a JSON serialized object into a JS object, so I can access its properties? 如何将javascript相同的函数带入可重用的函数中? - How do I take javascript identical functions and make then into a reusable function? 如何在每个循环中保存变量的值,以便以后可以使用这些值 - How do I save the value of a variable on each loop so I can use those values later
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM