简体   繁体   中英

Dropdown Div Doesn't Work

The following code does not drop down and up the div when clicked,It doesn't perform anything.But Im trying the exact code that works here http://jsfiddle.net/vNeXq/

<html>
<head>  
    <link rel="stylesheet" type="text/css" href="style.css">
    <script>
        $('#login').click(function(){
    $('#loginForm').slideToggle('fast');
});
    </script>
</head>
<body>
    <div id="loginWrapper">
        <a id="login">Login</a>
        <div id="loginForm">
            <form name="login" method="post">
                <input type="text" name="username" placeholder="Username" />
                <input type="password" />
                <input type="submit" value="Login" />
            </form>
        </div>
    </div>
</body>

CSS File

    #loginWrapper
    {
        width:400px;
        background-color:#F0F0F0;
    }
    #login 
    {
        cursor:pointer;
    }
    #loginForm 
    {
        display:none;
    }

Pretty sure you haven't included jQuery. The jQuery library is necessary for the script you want to run. You can download a copy of jQuery from here or you can use a copy hosted from Google . jQuery has to be included before the code you want to run.

<html>
<head>  
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="loginWrapper">
        <a id="login">Login</a>
        <div id="loginForm">
            <form name="login" method="post">
                <input type="text" name="username" placeholder="Username" />
                <input type="password" />
                <input type="submit" value="Login" />
            </form>
        </div>
    </div>
    <!-- INCLUDE JQUERY -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>

      $(document).ready(function(){
         $('#login').click(function(){
              $('#loginForm').slideToggle('fast');
          });
      }):

    </script>
</body>
</html>

If you have included jQuery in your project you maybe want to wrap your code with a function that executes when the page is loaded, so you are sure everything is there:

$(document).ready(function(){
   $('#login').click(function(){
        $('#loginForm').slideToggle('fast');
    });
})

You can try like:

    <script>
            $(document).ready(function(){
               $(document).on('click','#login',function(){
                    $('#loginForm').slideToggle('fast');
                });
            });

    </script>

It seems that are your not including the jQuery library in the head of the html file.

Include the jquery library (it should be before your script):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

In order for jQuery to modify the html document it must wait until the page is ready. You can do this by putting your function into:

$(document).ready(function() { 
     // your code here
});

So your script will end up looking like:

$(document).ready(function() {
      $('#login').click(function(){
        $('#loginForm').slideToggle('fast');
      });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM