我正在嘗試一些基本的Javascript與選擇元素和標籤,但它給我的問題。 具體來說,我得到這個錯誤在開發者工具使用Chrome或Firefox:無法調用方法'getElementsByTagName'爲空
遺漏的類型錯誤:無法調用空的方法「的getElementsByTagName」
奇怪的是,當我輸入的JavaScript命令到控制檯在瀏覽器上,它的工作沒有問題。 (選擇包裝專區內的p標籤)
這是我的html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<link rel="stylesheet" href="default.css" />
<script type="text/javascript" src="selection.js"></script>
</head>
<body>
<div id="wrapper">
<p>Hello, this is a paragraph</p>
<p>Another one!</p>
<p>This is yet another paragraph</p>
</div>
</body>
</html>
然後我的JavaScript:
window.onload = initP();
function initP() {
var para = document.getElementById('wrapper').getElementsByTagName('p');
}
好吧,我感到很蠢。謝謝!這就是訣竅:) – Flexicon
@ user3112167: - 不客氣! –