用于隐藏/取消隐藏面板的 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7497126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript function to hide/unhide panel
提问by Ghassan
I'm using asp.net(C#) under visual studio 2010
我在 Visual Studio 2010 下使用 asp.net(C#)
I have a panel that is by default set 2 be hidden( visible=false)
我有一个面板,默认设置为 2 隐藏(可见 = 假)
I need to create a JavaScript function that would be executed on a button click to make this panel visible if hidden and hidden if visible. and this should be client side, here is the code I have so far
我需要创建一个 JavaScript 函数,该函数将在单击按钮时执行,以使该面板在隐藏时可见,如果可见则隐藏。这应该是客户端,这是我到目前为止的代码
<script type=text/javascript>
function func1()
{
i need this code please
}
<asp:Panel ID="ResultsPanel" runat="server">
Some controls
</asp:Panel>
<asp:button id=button1 runat=server onclick=javascript:func1()>Hide/Unhide</asp:button>
回答by Amir Ismail
first you need to use OnClientClick
attribute instead of OnClick
for your button, and if that button does not run any server side code you can use html button instead of asp:Button
首先,您需要使用OnClientClick
属性而不是OnClick
按钮,如果该按钮不运行任何服务器端代码,您可以使用 html 按钮而不是asp:Button
<input type="button" onclick="func1();" value="Hide/Unhide">
you can use toggle
function in jquery
to hide/unhide your panel
您可以使用toggle
函数jquery
来隐藏/取消隐藏您的面板
function func1()
{
var mypanel = $('#<%=ResultsPanel.ClientID%>');
mypanel.toggle();
}
回答by Frank Allenby
Try this:
试试这个:
var Panel = document.getElementById("ResultsPanel");
if (Panel.style.display == "block" || Panel.style.display == "")
{
Panel.style.display = "none";
}
else
{
Panel.style.display = "block";
}
回答by nkm
If you are using jQuery, you may make use of following jQuery methods,
如果您正在使用 jQuery,您可以使用以下 jQuery 方法,