![](/img/trans.png)
[英]How do I access a DIV from javascript, if ASP.NET mangles its ID?
[英]How do I access the element ID in an asp.net masterpage with JavaScript?
我知道這個問題有幾個答案。
通常在線給出的答案涉及這樣做......
document.getElementById (<%=myElementID.ClientID %>);
而不是這個……
document.getElementById("myElementID");
但是,即使我采用第一種方式,我的 JavaScript 代碼仍然找不到該元素。 它告訴我元素未定義。
所以......只是為了測試......我試圖去掉我所有的 JavaScript 代碼並使用這樣的警告框訪問元素......
<script type="text/javascript">
alert(document.getElementById('<%=searchHyperLink.ClientID %>').value);
</script>
它仍然告訴我它的未定義。
但是當我查看頁面源時,它並沒有被定義。 <%=searchHyperLink.ClientID %> 呈現的 id 與我要查找的控件的 id 完全匹配。
有什么建議么?
為了擴展 Evan 的評論,如果您在文檔排序中位於元素之前的腳本標記中執行document.getElementById
,它將返回undefined
。 試試這個:
<html>
<head>
<script>
function postLoadFn() {
alert(document.getElementById('<%=searchHyperLink.ClientID %>').innerHTML);
}
</script>
</head>
<body onload="postLoadFn();">
<!-- Your markup here -->
</body>
</html>
我在我的項目 asp.net masterpage(home.master) 中使用parent
訪問更高級別。
代替:
document.getElementById (<%=myElementID.ClientID %>);
利用:
parent.window.document.getElementById (<%=myElementID.ClientID %>);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.