[英]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 . 当我单击打开和关闭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>
I have read the answers in comments. 我已阅读评论中的答案。
<a onClick="toggle(); return false;" />
For, href="" you cann add # like 对于href =“”,您无法添加#喜欢
<a href="#" onClick="toggle(); return false;">
Thanks 谢谢
You are using asp:LinkButton
which causes postback. 您正在使用
asp:LinkButton
导致回发。 Use html a
instead to prevent it. 使用html
a
来防止它。
<a href="#" onClick="toggle();">
And if you want to show/hide your div use jQuery toggle. 如果要显示/隐藏div,请使用jQuery切换。
<a href="#">
<script>
$("a").click(function(){
$("#showw").toggle();
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.