![](/img/trans.png)
[英]TypeError: document.getElementById(…)[0] is undefined
[英]document.getElementById throws TypeError
我對 Javascript 有點陌生。 我正在編寫這個虛擬腳本來測試我的大頁面需要什么。 我想,最簡單的是,獲取 href 的值,然后更改 href 的值,然后再次查看它。 為了簡單起見,現在我已經刪除了更改 href 值的部分,並且只調用了兩次屬性值。 但是第二次訪問屬性值時,它顯示了我在下面發布的錯誤。 我發布了兩個錯誤日志,一個來自 Firebug,另一個來自 Dragonfly(來自 Opera)。
任何幫助將不勝感激。 謝謝。
我對腳本的另一個問題是它永遠不會完成加載。 我總是看到(在 Firefox 3.6.8 中)標簽標題中的小加載標志轉來轉去。 它不會打擾我這么多,但如果有人有想法請告訴我。
<!-- This file is used to set attribute value for href. -->
<html>
<head>
<script type="text/javascript">
function hrefvalue()
{
var thisthang = document.getElementById("1").childNodes[0].getAttribute("href");
document.write(thisthang);
document.write("\n");
var newnew21 = document.getElementById("1").childNodes[0].getAttribute("href");
document.write(newnew21);
}
</script>
</head>
<body>
<div id="1"><a href="focusdude.htm">click here</a></div>
<button type="button" onclick="hrefvalue()">Click me instead</button>
</body>
</html>
錯誤日志:
1. Firebug -> document.getElementById("1") 是 null
2.蜻蜓->
未捕獲的異常:TypeError:無法將 'document.getElementById("1")' 轉換為 object
在第 8 行,hrefvalue() 中的第 0 列引發錯誤
文件://localhost/D:/Chicago%20pics/website%20pics/website%20root/trial5.htm:
var newnew21 = document.getElementById("1").childNodes[0].getAttribute("href");
從 (event) 中的第 1 行第 0 列調用:hrefvalue()
再次感謝所有的魚:D
document.write
將丟棄未打開的文檔。 當瀏覽器解析</html>
時,文檔被關閉。 由於 function 運行onclick
,它將在您嘗試寫入之前關閉。 使用DOM方法用JS修改HTML。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.