2

我正在尝试使用 jQuery/JavaScript 制作下拉菜单,但我的菜单不会显示任何内容。

这是我的代码:

JavaScript:

function toggleMenu(target){
    if($(target).css("display")=='none'){
        $("#drop_down, drop_menu").hide(500);
    $(target).toggle(500);
}

头文件.php

<?php

?>
<div id="header" class="absolute" >
    <ul>
        <li class="float_left"><p class="pointer" onclick="toggleMenu('#drop_search')">Search</p></li>
        <li class="float_left"><p class="pointer" onclick="toggleMenu('#drop_login')">Log in</p></li>
        <li class="float_left"><p class="pointer" onclick="toggleMenu('#drop_chart')">My Cart</p></li>
        <li class="float_left"><p class="pointer" onclick="toggleMenu('#drop_menu')">Menu</p></li>
    </ul>
     <div class="clear_both"></div>
     <br/>    
    <div id="drop_down"class="right">
        <div id="drop_search" class="hidden">
            <p>Search</p>
        </div>
        <div id="drop_login" class="hidden">
            <p>login</p>
        </div>
        <div id="drop_chart" class="hidden">
            <p>cart</p>
        </div>
        <div id="drop_menu" class="hidden">
            <p>menu</p>
        </div>
    </div>
</div>

抱歉,css很长,我只是想把它打包成一个文件

样式.css

body{
    position: relative;
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;
    font-family: sans-serif;
}
body table{
    border-collapse: collapse;
    width: 100%;
}
body th, body tr, body td{
    padding: 0;
}
body h1, body h2,body h3,body h4,body h5,body h6,body p{
    margin: 0px;

}
body p{
    font-size: 12px;
}
body h6{
    font-size: 14px;
}body h5{
    font-size: 16px;
}body h4{
    font-size: 18px;
}body h3{
    font-size: 20px;
}body h2{
    font-size: 24px;
}body h1{
    font-size: 28px;
}
body ul,body li{
    list-style: none;
    margin: 0px;
    padding: 0px;
}
body img{
    border: none;
}
body hr{
    margin: 0px;
    border: none;
    height: 1px;
}
body a:link{
    text-decoration: none;
}
body a:visited{}
body a:hover{}
body .relative{
    position: relative;
}
body .absolute{
    position: absolute;
}
body .fixed{
    position: fixed;
}
body .float_left{
    float: left;
}
body .float_right{
    float: right;
}
body .clear_both{
    clear: both;
}
body input, body select, body textarea{
    border-radius: 5px;
}
body .input_text{}
body .input_select{
    border-radius: 5px;
    font-size: 11px;
}
body .input_option{
    background-color: #f6f6f6;
    color: #333333;
}
body .input_option_inactive{
    background-color: #f6f6f6;
    color: #888888;
}
body .input_submit{
    background: url("../images/submit.png");
    border: none;
    font-weight: bold;
    color: #efefef;
    cursor: pointer;
    border-radius: 20px;
    height: 20px;
}
body .input_submit:hover{
    color: #ffffff;
    text-shadow: 0 0 3px #efefef;
}
body .input_submit_inactive{
    background: url('#');
    border:none;
}
body .input_submit_inactive:hover{
    color: #333333;
    text-shadow: 0 0 3px #434343;
}
body .input_textarea{}
body .top{
    vertical-align: top;
}
body .middle{
    vertical-align: middle;
}
body .bottom{
    vertical-align: bottom;
}
body .left{
    text-align: left;
}
body .center{
    text-align: center;
}
body .right{
    text-align: right;
}
body .spacer{
    width: 10px;
    height: 10px;
}
body .bold{
    font-weight: bold;
}
body .italic{
    font-style: italic;
}
body .strip{
    text-decoration: line-through;
}
body .hidden{
    display: none;
}
body .show{
    display: block;
}
body .pointer{
    cursor: pointer;
}
body .scroll-y{
    overflow-y: scroll;
}
#whole{
    width: inherit;
    height:100%; 
    margin-left: auto;
    margin-right: auto;
    vertical-align: top;
}
.black{color: #000000;}
.white{color: #ffffff;}
.red{color: #ff0000;}
.green{color: #00ff00;}
.blue{color: #0000ff;}
.yellow{color: #ffff00;}
.toska{color: #00ffff;}
.pink{color: #ff00ff;}

header.css

#header{
    right: 0;
    top: 0;
}
 #header li{ height: 20px;
    background: #960000;
}
#header li p{
    margin: 0 15px;
    color: #cccccc;
    height: 12px;    
    line-height: 20px;
    font-weight: bold;
}
#header li p:hover{
    color: #f6f6f6;
}
#header #drop_down{
    background: url('../images/black20.jpg');
}

任何人都可以帮忙吗?

4

3 回答 3

1

删除您的class="hidden"并将默认设置为style="display: none"(或在页面加载时隐藏)。

于 2012-12-04T13:18:05.870 回答
0

此代码可能会根据您的要求工作。我没有更改您的代码,只是完全删除了 class="hidden" 。

注意:你没有提到你的 CSS,所以我假设你定义的类名。

如果我落后于某个地方,请告诉我......

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
#header {
    border:1px solid red;
    width:100%;
}
.pointer {
    cursor:pointer;
}
.absolute {
    position:absolute;
}
.float_left {
    float:left;
}
.right {
    float:right;
}
.clear_both {
    clear:both;
}
</style>
</head>
<body>
<div id="header" class="absolute">
  <ul>
    <li class="float_left">
      <p class="pointer" onclick="toggleMenu('#drop_search');">Search</p>
    </li>
    <li class="float_left">
      <p class="pointer" onclick="toggleMenu('#drop_login')">Log in</p>
    </li>
    <li class="float_left">
      <p class="pointer" onclick="toggleMenu('#drop_chart')">My Cart</p>
    </li>
    <li class="float_left">
      <p class="pointer" onclick="toggleMenu('#drop_menu')">Menu</p>
    </li>
  </ul>
  <div class="clear_both"></div>
  <br/>
  <div id="drop_down" class="right">
    <div id="drop_search">
      <p>Search</p>
    </div>
    <div id="drop_login">
      <p>login</p>
    </div>
    <div id="drop_chart">
      <p>cart</p>
    </div>
    <div id="drop_menu">
      <p>menu</p>
    </div>
  </div>
</div>
<script type="text/javascript">

$("#drop_down div").hide();
function toggleMenu(target)
{
    if($(target).css("display") != 'none')
    {
        $(target).hide(500);
    }
    else
    {
        $("#drop_down div").hide(500);
        $(target).show(500);
    }
}
</script>
</body>
</html>
于 2012-12-05T09:36:41.507 回答
0

您的函数中有一个 sintaxe 错误
尝试使用此函数

function toggleMenu(target){
    if($(target).is(":hidden")){
        $("#drop_down div").hide(500);
    }
    $(target).toggle(500);
}

并给我一些反馈...

于 2012-12-05T22:28:05.650 回答