admin管理员组

文章数量:1392003

I'm currently trying to make my menu stay when you click on the menu, i mean so that the dropdown menu stays after clicking on it. That is code i've already tried(Javascript down below), but for some reason it wont work for me in reality. So I'm looking for help now.

Fiddle with style > /

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
".dtd">

<html xmlns=""xml:lang="sv">
<head>
  <title>Nösnäs</title>

        <link rel="stylesheet" type="text/css" href="screen.css" />
<script src='.10.2/jquery.min.js'></script>
        <script src="script.js"></script>
</head>
<body>

<button class="nav-button">Toggle Navigation</button>

<ul class="nav">
<li class="program"><a href="#">Program</a>
<div class="second nav">
  <ul>
<li><a href="#">Teknik</a></li>
<li><a href="#">Naturvetenskap</a></li>
<li><a href="#">El</a></li>
</ul></li>
</div>
  <li><a href="#">Nösnäs</a></li>
  <li><a href="#">Schema</a></li>
  <li><a href="#">Matsal</a></li>
   </ul>






</body>
</html>






console.log("hej");

$(".program").on('click',function () {
console.log("hejssss");
                $('li div ul').toggle('.close');
                console.log("hejs");
                $('li div ul').show();

        });

I'm currently trying to make my menu stay when you click on the menu, i mean so that the dropdown menu stays after clicking on it. That is code i've already tried(Javascript down below), but for some reason it wont work for me in reality. So I'm looking for help now.

Fiddle with style > http://jsfiddle/hRxRG/

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3/1999/xhtml"xml:lang="sv">
<head>
  <title>Nösnäs</title>

        <link rel="stylesheet" type="text/css" href="screen.css" />
<script src='http://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js'></script>
        <script src="script.js"></script>
</head>
<body>

<button class="nav-button">Toggle Navigation</button>

<ul class="nav">
<li class="program"><a href="#">Program</a>
<div class="second nav">
  <ul>
<li><a href="#">Teknik</a></li>
<li><a href="#">Naturvetenskap</a></li>
<li><a href="#">El</a></li>
</ul></li>
</div>
  <li><a href="#">Nösnäs</a></li>
  <li><a href="#">Schema</a></li>
  <li><a href="#">Matsal</a></li>
   </ul>






</body>
</html>






console.log("hej");

$(".program").on('click',function () {
console.log("hejssss");
                $('li div ul').toggle('.close');
                console.log("hejs");
                $('li div ul').show();

        });
Share Improve this question edited Mar 11, 2014 at 8:57 Rory Galloway asked Mar 11, 2014 at 8:03 Rory GallowayRory Galloway 853 silver badges8 bronze badges 2
  • What exactly is your problem? This works for me: jsfiddle/vCNks – DonJuwe Commented Mar 11, 2014 at 8:07
  • @DonJuwe click on submenu and check – Hamed mayahian Commented Mar 11, 2014 at 8:10
Add a ment  | 

2 Answers 2

Reset to default 5

Try this markup:

<ul class="nav">
    <li class="program"><a href="#">Program</a></li>
    <div class="secondNav">
        <ul>
            <li><a href="#">Teknik</a></li>
            <li><a href="#">Naturvetenskap</a></li>
            <li><a href="#">El</a></li>
        </ul>
    </div>
    <li><a href="#">Nösnäs</a></li>
    <li><a href="#">Schema</a></li>
    <li><a href="#">Matsal</a></li>
</ul>

And this Javascript:

$('li.program').on('click', function() {
    $('.secondNav').toggle();
});

Here is the DEMO!

Your HTML code is not valid change it as follows :

<ul class="nav">
   <li class="program"><a href="#">Program</a>
   <div class="second nav">
     <ul>
       <li><a href="#">Teknik</a></li>
       <li><a href="#">Naturvetenskap</a></li>
       <li><a href="#">El</a></li>
     </ul></div></li>
  <li><a href="#">Nösnäs</a></li>
  <li><a href="#">Schema</a></li>
  <li><a href="#">Matsal</a></li>
</ul>

and the JS you need to modify for sub menu click event like this :

$(".program").on('click',function () {
   $('li div ul').toggle('');
});
$('.nav > li div ul li a').click(function(e) {
   e.stopPropagation();
});

with css check this : http://jsfiddle/hRxRG/1/

本文标签: javascriptMaking dropdown menu stay after clickingStack Overflow