我试图在外部脚本中访问 asp:panel 以使其不可见,但它似乎不起作用。当脚本位于 .aspx 文件中时,它可以正常工作。有什么建议吗?
在 .aspx 文件中
<script src="App_Themes/custom.js"></script>
<asp:Button ID="descriptionButton" Text="Description" runat="server" OnClientClick="descButton(); return false;" />
<asp:Panel ID="desciptionPanel" runat="server">
///random stuff
<asp:panel>
在 custom.js 文件中
function descButton() {
var desc = document.getElementById('<%=desciptionPanel.ClientID%>');
desc.style.visibility = "visible";
desc.style.height = "800px";
}
提前致谢
最佳答案
将其分成两部分。
首先,在服务器的aspx中,留下ID,以便以后可以重复使用
<script>
window.panelID = '<%= whatever.ClientID %>';
</script>
然后,在外部脚本中,只需使用 ID
function externalJSfunction() {
var desc = document.getElementById(window.panelID);
}
关于javascript - 当脚本是外部时 getElementByID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28487266/