如何使 JavaScript 菜单更实用
我只是想知道哪种方法是使该菜单动态化的最佳实践。我不想为每个元素创建一个函数。 我应该将它们推入数组然后循环遍历它们吗?
<div id="nav">
<div id="button1"></div>
<div id="button2"></div>
<div id="button3"></div>
<div id="button4"></div>
</div>
<div id="navHover">
<div id="hoverButton1"></div>
<div id="hoverButton2"></div>
<div id="hoverButton3"></div>
<div id="hoverButton4"></div>
</div>
<script type="text/javascript">
var buttonOne = document.getElementById('button1');
var buttonOneHover = document.getElementById('hoverButton1');
buttonOne.addEventListener('mouseover', buttonOneBlock, false);
buttonOne.addEventListener('mouseout', buttonOneNone, false);
buttonOneHover.addEventListener('mouseover', buttonOneBlock, false);
buttonOneHover.addEventListener('mouseout', buttonOneNone, false);
function buttonOneBlock() {
var buttonOneHover = document.getElementById('hoverButton1');
buttonOneHover.style.display = 'block';
}
function buttonOneNone() {
var buttonOneHover = document.getElementById('hoverButton1');
buttonOneHover.style.display = 'none';
}
</script>
#nav {
width: 960px;
height: 20px;
background-color: white;
margin: auto;
}
#button1, #button2, #button3, #button4 {
width: 100px;
height: 20px;
background-color: red;
float: left;
margin-left: 10px;
}
#navHover {
width: 960px;
height: 20px;
background-color: white;
margin: auto;
}
#hoverButton1, #hoverButton2, #hoverButton3, #hoverButton4 {
width: 100px;
height: 20px;
background-color: orange;
float: left;
margin-left: 10px;
display: none;
}
I wonder simply which way is the best practice to make this menu dynamic. I don't want to make a function for each Element.
Should I push them into an Array then loop through them?
<div id="nav">
<div id="button1"></div>
<div id="button2"></div>
<div id="button3"></div>
<div id="button4"></div>
</div>
<div id="navHover">
<div id="hoverButton1"></div>
<div id="hoverButton2"></div>
<div id="hoverButton3"></div>
<div id="hoverButton4"></div>
</div>
<script type="text/javascript">
var buttonOne = document.getElementById('button1');
var buttonOneHover = document.getElementById('hoverButton1');
buttonOne.addEventListener('mouseover', buttonOneBlock, false);
buttonOne.addEventListener('mouseout', buttonOneNone, false);
buttonOneHover.addEventListener('mouseover', buttonOneBlock, false);
buttonOneHover.addEventListener('mouseout', buttonOneNone, false);
function buttonOneBlock() {
var buttonOneHover = document.getElementById('hoverButton1');
buttonOneHover.style.display = 'block';
}
function buttonOneNone() {
var buttonOneHover = document.getElementById('hoverButton1');
buttonOneHover.style.display = 'none';
}
</script>
#nav {
width: 960px;
height: 20px;
background-color: white;
margin: auto;
}
#button1, #button2, #button3, #button4 {
width: 100px;
height: 20px;
background-color: red;
float: left;
margin-left: 10px;
}
#navHover {
width: 960px;
height: 20px;
background-color: white;
margin: auto;
}
#hoverButton1, #hoverButton2, #hoverButton3, #hoverButton4 {
width: 100px;
height: 20px;
background-color: orange;
float: left;
margin-left: 10px;
display: none;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个简单的概括:
Here’s a straightforward generalization of that: