简体   繁体   中英

jquery fade doesn't seem to work

I'm trying to add a div that fades in/out when i press a button. this is the code in my ASP head:

<script type="text/javascript">
   $(this.document).ready(function () {
      $("#<%=this.button1.ClientID %>").click(function () {
         $("#mydiv").fadeToggle();
      });
   });
</script>

and then this later down in body:

<asp:Button ID="button1" runat="server" Text="button" />

<div id="mydiv" style="display:none">
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
Load page with fade in effect...<br />
</div>

When I test it, the button is there but nothing happens when i press it. when i SPAM press it repetedly, i can see the div starting to fade in/out, but just flickering for a milliseccond.

the button is always make post back, so you did not see the javascript running because your page is reloaded.

You can use the return false; or the preventDefault() to avoid the post back and see your client side effect.

<script type="text/javascript">
   $(this.document).ready(function () {
      $("#<%=this.button1.ClientID %>").click(function (event) {
         // not let it make post back
         event.preventDefault();

         $("#mydiv").fadeToggle();
      });
   });
</script>

use

$("selector").fadeToggle("slow", "linear");

it will fade with slow effect

Specify a duration for .fadeToggle() . The default is 400, which is pretty quick. .fadeToggle(1000) will take a second.

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