javascript 使用javascript将面板设置为可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16635285/
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
Setting a panel to visible using javascript
提问by Arianule
How can I make a <asp:Panel>
visible using javascript?
如何<asp:Panel>
使用javascript使可见?
I have done the following but get an error (Cannot Read property style of null)
我已完成以下操作,但出现错误(无法读取 null 属性样式)
<asp:Panel runat="server" id="panNonAfricanCountries" Visible="false">
var panNonAfricaDropDown = document.getElementById("panNonAfricanCountries")
if (dropDownFirst == "Yes") {
panNonAfricaDropDown.style.visibility = "visible";
}
回答by Aristos
The Visible="false"
on an asp.net control have as result to not render the controlon the page.
因此Visible="false"
,asp.net 控件不会在页面上呈现控件。
What you try to do here is to render it, but with css style to have it hidden from the user until using javascript show it. To archive that do not use the Visible, but set a style or a css to your Panel.
您在这里尝试做的是渲染它,但使用 css 样式使其对用户隐藏,直到使用 javascript 显示它。归档不使用 Visible,但为您的面板设置样式或 css 的文件。
<asp:Panel ID="PanelId" runat="server" Visible="true" style="visibility:hidden" >
Some Content here...
</asp:Panel>
The asp.Panel
is render as div
and your html on the page will probably as:
该asp.Panel
是呈现为div
你的HTML页面上会可能是因为:
<div id="PanelId" style="visibility:hidden">
Some Content here...
</div>
and I say probably because we do not know for sure how the Id is rendered. To get it we use the PanelId.ClientID
and your final javascript code will be:
我这么说可能是因为我们不确定 Id 是如何呈现的。为了得到它,我们使用PanelId.ClientID
你的最终 javascript 代码将是:
var panNonAfricaDropDown = document.getElementById("<%=PanelId.ClientID%>");
if (dropDownFirst == "Yes" && panNonAfricaDropDown) {
panNonAfricaDropDown.style.visibility = "visible";
}
回答by syazdani
ASP.NET mangles the names of elements that run on the server. You will have to find the mangled name and then do document.getElementById on that name.
ASP.NET 会修改在服务器上运行的元素的名称。您必须找到损坏的名称,然后对该名称执行 document.getElementById。
Alternatively, you can use the ClientIDMode property of asp:panel to turn off the mangling (http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx)
或者,您可以使用 asp:panel 的 ClientIDMode 属性关闭重整(http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx)