繁体   English   中英

当我单击打开和关闭div按钮时,我想通过JavaScript切换div

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

当我单击打开和关闭div的按钮时,我想通过JavaScript toggle div,这是我编写的代码,但是当我单击按钮时,div消失了,我认为这是回发的。

  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> 

我已阅读评论中的答案。

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

对于href =“”,您无法添加#喜欢

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

谢谢

您正在使用asp:LinkButton导致回发。 使用html a来防止它。

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

如果要显示/隐藏div,请使用jQuery切换。

<a href="#">

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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