gpt4 book ai didi

javascript - 我不断收到 Uncaught ReferenceError : dropMenu is not defined and Uncaught TypeError: Cannot read property 'style' of null

转载 作者:行者123 更新时间:2023-11-28 03:28:39 27 4
gpt4 key购买 nike

我试图用子菜单制作导航栏,但我一直收到 dropMenu 未定义,当我将鼠标悬停在两个巴黎链接上时,我收到 Uncaught TypeError: Cannot read property 'style' of null 不知道是什么原因造成的这是我的代码html

<html>
<link type="text/css" rel="stylesheet" href="vacation.css"/>
<head>
<title>Paris</title>
</head>
<body>
<div id="menus">
<div id ="parismenu">
<a href="#" onmouseover="dropMenu('dropmenu1')">Paris</a>
<div id="dropmenu1" class="dropmenus">
<a href="#">apple</a>
</div>
</div>
<div id ="disneymenu">
<a href="#" onmouseover="dropMenu('dropmenu2')">Paris</a>
<div id="dropmenu2" class="dropmenus">
<a href="#">apple</a>
</div>
</div>
</div>
</body>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="vacation.js"></script>
</html>

CSS

div#menus{
margin-left:16px;
}
div#menus> div{
float:left;
margin:0px 20px;
}
div.dropmenus{
display:none;
position:absolute;
top:100px;
width:120px;
background:teal;
z-index:2;
padding:4px;
border: blue 3px solid;
border-top:none;
border-radius:5px 5px 5px 5px;
}
div.dropmenus>a{
display:block;
margin:4px;
padding:7px;
font-size:14px;
text-decoration:none;
background:blue;
border-radius:3px;
color:red;
}
div#dropmenu1{
left:24px;
}
div#dropmenu2{
left:116px;
}

JavaScript

 var fade=function(){
return{
init:function(id,flag,target){
this.elem=document.ElementById(id);
clearInterval(this.elem.si);
this.target=target ? target : flag ? 100 : 0;
this.flag = flag||1;
this.alpha = this.elem.stle.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
this.elem.si=setIntterval(function(){fade.fadep()},20);
},
fadep:function(){
if(this.alpha==this.target){
clearInterval(this.elem.si);
}
else{
var value=Math.round(this.alpha + ((this.target - this.alpha) * .05)) +(-1 * this.flag);
this.elem.style.opacity=value/100;
this.elem.style.filter='alpha(opacity=' + value + ')';
this.alpha=value
}}}}();

var menu= ["dropmenu1","dropmenu2","dropmenu3"];
function dropMenu(x){
for(var m in menu){
if(menu[m] != x){
document.getElementById(menu[m]).style.display="none";
}}
if(document.getElementById(x).style.diplay=="block"){
fade.init(x,1);
}
else{
fade.init(x,0)}}

最佳答案

你的数组包含三个 id 而你的 html 只有前两个 - 所以

document.getElementById(menu[m])

如果 m === 2 将是未定义的

因此您需要从菜单数组中删除“dropmenu3”。

此外,这一行:

this.elem=document.ElementById(id);

应该是

this.elem=document.getElementById(id);

关于javascript - 我不断收到 Uncaught ReferenceError : dropMenu is not defined and Uncaught TypeError: Cannot read property 'style' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19415405/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com