无法创建下拉菜单 - jquery

时间:2013-09-17 21:53:47

标签: javascript jquery css menu

我正在尝试为我的网站制作一个下拉菜单。 这是我的代码:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>Gowemto</title>
<link rel="stylesheet" type="text/css" href="Website%20style.css">
<link rel="stylesheet" type="text/css" href="styles/home.css">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style>
.menu {
    -moz-box-shadow:inset 0px 1px 0px 0px #f4cafc;
    -webkit-box-shadow:inset 0px 1px 0px 0px #f4cafc;
    box-shadow:inset 0px 1px 0px 0px #f4cafc;
    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #eea1fc), color-stop(1, #d441ee) );
    background:-moz-linear-gradient( center top, #eea1fc 5%, #d441ee 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#eea1fc', endColorstr='#d441ee');
    background-color:#eea1fc;
    text-indent:0;
    border:1px solid #dd5df4;
    display:inline-block;
    color:#FFFFFF;
    font-family:Arial;
    font-size:14px;
    font-weight:bold;
    font-style:normal;
    height:20px;
    line-height:20px;
    text-decoration:none;
    text-align:center;
    text-shadow:1px 1px 0px #b63dcc;
}
.menu:hover {
    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #d441ee), color-stop(1, #eea1fc) );
    background:-moz-linear-gradient( center top, #d441ee 5%, #eea1fc 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#d441ee', endColorstr='#eea1fc');
    background-color:#d441ee;
    text-decoration:none;
    text-align:center;
    text-shadow:1px 1px 0px #b63dcc;
    color: #FFFFFF;
}.menu:active {
    position:relative;
    top:1px;
color: #FFFFFF;}
a.menu:visited {text-shadow:1px 1px 0px #b63dcc;
color: #FFFFFF;}
#droptwo{background-color:#FFFFFF;border:1px groove #FF00FF; position: absolute; width: 126px; z-index: 100; left: 325px; top: 117px;} 
ul
  {
  list-style:none;
  padding:0px;
  margin:0px
  }

ul li
  {
  display:inline;
  float:left;
  }

ul li a
  {
  color:#800080;
  background:#FFFFFF;
  margin-right:5px;
  font-weight:bold;
  font-size:12px;
  font-family:verdana;
  text-decoration:none;
  display:block;
  width:126px;
  height:24px;
  line-height:25px;
  text-align:center;
  border: 1px solid #FF00FF;
  }

ul li a:hover
  {
  color:#FFFFFF;
  background:#EE80FF;
  font-weight:bold;
  text-decoration:none;
  display:block;
  width:126px;
  text-align:center;
  border: 1px solid #800080;
  }

</style>
</head>
<body>
<table border="0" width="100%" id="table3" cellspacing="4" cellpadding="0" style="height:945px;margin: 0">
    <tr style="height: 65px;">
        <td valign="top" colspan="4" id="headtd" style='height: 87px;line-height:28px;'>
        <script>
$(document).ready(function() {
  $('#droptwo').css({'left':'-1000px'});
});

$("#droptwo").hover(
  function() {
    $('#droptwo').css({'left':'325px'});       
  },
  function() {
    $('#droptwo').css({'left':'-1000px'});
  }
);
</script>
<img border="0" src="Header.jpg" width="1322" height="80"><br>
<b><font face="Tempus Sans ITC">&nbsp;<font color="#800080"><a href="home.php" class="menu" style="color:#FFFFFF;">&nbsp;Home&nbsp;</a>&nbsp;&nbsp;
                <a href="profile.php" class="menu" style="color:#FFFFFF;">&nbsp;Profile&nbsp;</a>&nbsp;&nbsp;
                <a href="topics.php" class="menu" style="color:#FFFFFF;">&nbsp;Topics&nbsp;</a>&nbsp;&nbsp;<a href="inbox.php" class="menu" style="color:#FFFFFF;">&nbsp;Inbox&nbsp;</a>&nbsp;
&nbsp;<a href=messenger.php class="menu" style="color:#FFFFFF;">&nbsp;Chat&nbsp;</a>&nbsp;
&nbsp;<span id="comfate"><a href="javascript:void(0);" onmouseover=" $('#droptwo').css({'left':'325px'});" class="menu" style="color:#FFFFFF;">&nbsp;Account&nbsp;</a></span><div id="searc" style='position: absolute; width: 220px; height: 27px; z-index: 15; left: 920px; top: 35px;'>
<form method="GET" action="search.php">
<input type=text name=srch id=srch placeholder="Search for people or topics.." size="15" maxlength="500">
<input type="submit" value="Go"></form></div>
<div id="droptwo">
  <ul>
 <li>
  <a href="editprofile.php?user=<?php echo $showusername;?>">Edit profile</a>
  <a href="settings.php">Settings</a>
  <a href="help.php">Help</a>
  <a href="logout.php">Logout</a>
  </li>
 </ul>
</div>
</font></font></b>
</tr>
            <tr>
                <td rowspan="2" bgcolor="#FFFFFF" valign="top" id="extras" >
                </td>
            </tr>
</table>
<?php require "footer.php" ;?>  

当我将鼠标悬停在导航中的帐户菜单上时,会出现下拉菜单。但是当我离开下拉菜单时,它并没有消失。什么错误我的代码请帮忙??

1 个答案:

答案 0 :(得分:2)

只有你的内联jQuery正在运行,而不是<script>代码块中的jQuery。

因此,请按如下方式修改锚标记:

<a href="javascript:void(0);" onmouseover=" $('#droptwo').css({'left':'325px'});"  onmouseout="$('#droptwo').css({'left':'-1000px'});" class="menu" style="color:#FFFFFF;">&nbsp;Account&nbsp;</a>

它会起作用。


要使jQuery代码块正常工作,请首先从锚标记中删除内联javascript:

<a href="javascript:void(0);" class="menu" style="color:#FFFFFF;">&nbsp;Account&nbsp;</a>

然后修改你的jQuery代码块:

$(document).ready(function() {

    $('#droptwo').css({'left':'-1000px'});

    $("span > a").hover(
        function() {
            $('#droptwo').css({'left':'325px'});       
        },
        function() {
            $('#droptwo').css({'left':'-1000px'});
        }
    );
}); //END document.ready
相关问题