簡體   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