2015-07-20 243 views
0

我正在爲我的網站做一個下拉菜單,現在它的工作,但當我再次點擊鏈接時,我得到的問題,它不工作,因爲我不擅長JavaScript,所以我需要來自你們大家的一些幫助。謝謝你的時間。菜單點擊下拉JS

這裏我的代碼:

var hideall = $('#woman,#man,#device,#health,#living,#device'); 
 

 
    $('#woman-li').click(function() { 
 
     $(hideall).hide(), $('#woman').show(); 
 
    }); 
 

 

 
    $("#man-li").click(function() { 
 
     $(hideall).hide(), $('#man').show(); 
 
    }); 
 

 

 
    $("#health-li").click(function() { 
 
     $(hideall).hide(), $('#health').show(); 
 
    });
#woman, 
 
    #man, 
 
    #health, 
 
    #device, 
 
    #living { 
 
     display: none; 
 
    }
<div id='cssmenu'> 
 
    <ul> 
 
    <li class='active'><a href='#'><span>Home</span></a> 
 
    </li> 
 
    <li> 
 
     <a href="#"> <span id="woman-li">Woman</span> 
 
     </a> 
 
    </li> 
 
    <li> 
 
     <a href="#"> <span id="man-li">Man</span> 
 
     </a> 
 
    </li> 
 
    <li> 
 
     <a href="#"> <span id="health-li">Health</span> 
 
     </a> 
 
    </li> 
 
    </ul> 
 

 

 
    <div id='woman'> 
 

 
    <div class="tb-container"> 
 
     <div class="tb-head">face</div> 
 
     <div class="tb-content"> 
 
     <a href="#"> 
 
      <p>face</p> 
 
     </a> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <div id='man'> 
 

 
    <div class="tb-container"> 
 
     <div class="tb-head">face</div> 
 
     <div class="tb-content"> 
 
     <a href="#"> 
 
      <p>face</p> 
 
     </a> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <div id='health'> 
 

 
    <div class="tb-container"> 
 
     <div class="tb-head">face</div> 
 
     <div class="tb-content"> 
 
     <a href="#"> 
 
      <p>face</p> 
 
     </a> 
 
     </div> 
 
    </div> 
 
    </div>

+0

你的意思是說,當*不能很好的工作*?請編輯您的問題以詳細描述當前和期望的行爲。 – dhh

+0

你也可以創建[JSFiddle](http://jsfiddle.net/),所以我們可以很容易地跳進來看看發生了什麼 – robjez

回答

1

根據我的經驗,這是非常容易使用列表項,然後具有指導您的頁面(更整潔的造型的onclick處理程序)。也許這樣的事情是你追求的目標? https://jsfiddle.net/Domination/erdhvjvm/7/

HTML:

<div id='cssmenu'> 
    <ul> 
     <li id='home' class='active'>Home 
      <div>Face</div> 
     </li> 
     <li id='woman'>Woman 
      <div>Face</div> 
     </li> 
     <li id='man'>Man 
      <div>Face</div> 
     </li> 
     <li id='health'>Health 
      <div>Face</div> 
     </li> 
    </ul> 
</div> 

CSS:

#cssmenu ul{ 
    border-top:1px solid black; 
    padding:0; 
} 
#cssmenu li{ 
    background:red; 
    border:1px solid black; 
    border-top:0; 
    cursor:pointer; 
    list-style-type:none; 
    padding:0.5em; 
} 
#cssmenu li.active{ 
    background:green; 
} 
#cssmenu div{ 
    margin-left:15px; 
} 

JS:

//Hides all initially 
$('#cssmenu ul li div').hide(); 

//On click of one of the list items 
$('#cssmenu ul li').click(function(e){ 
    if (e.target == this){ //If you've actually clicked on it (not a child of it) 

     //Stops all previous animations 
     $('#cssmenu ul li div').stop(); 

     //Slides all others up 
     $(this).siblings().find('div').slideUp(); 

     //Removes class from others 
     $(this).siblings().removeClass('active'); 

     //Slides selected down or up (toggles it) 
     $(this).find('div').slideToggle(); 

     //Adds class to the element 
     $(this).addClass('active'); 
    } 
}); 

//On click on the children of the menu 
$('#cssmenu ul li div').click (function(){ 
    alert("You clicked on a child"); 
    alert("Go to link: " + $(this).attr('link')) 

    //Uncomment to enable links 
    //window.location.href = $(this).attr('link'); 
})