简体   繁体   中英

I want to toggle div by JavaScript when i click on the button open and close the div

I want to toggle div by JavaScript when I click on the button open and close the div , this my code I make but when i click on the button the div disappear I think it is from post-back .

  var toggle = function () { var mydiv = document.getElementById('showw'); if (mydiv.style.display === 'block' || mydiv.style.display === '') { mydiv.style.display = 'none'; } else { mydiv.style.display = 'block' } <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="toggle(); return false;">Amenities</asp:LinkButton> <div id="showw" runat="server" style="display:none;background-color:Blue; width:100px;height:100px;"> <asp:Label ID="lblAmiinties" runat="Server" CssClass="shbe_h2" Text='<%# Eval("Amenities") %>' Visible="true"></asp:Label> <asp:Label ID="lblAmiintiesTxt" runat="Server" Text="<%$ Resources:Resource,Amenities %>"></asp:Label> <br /> </div> 

I have read the answers in comments.

<a onClick="toggle(); return false;" />

For, href="" you cann add # like

<a href="#" onClick="toggle(); return false;">

Thanks

You are using asp:LinkButton which causes postback. Use html a instead to prevent it.

<a href="#" onClick="toggle();">

And if you want to show/hide your div use jQuery toggle.

<a href="#">

<script>
    $("a").click(function(){
      $("#showw").toggle();
    });
</script>

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