我正在尝试了解document
对象类型的使用并挑战自己,我正在尝试隐藏所有div
标签。当我执行该函数时,我返回了错误Uncaught TypeError: Cannot set property 'display' of undefined
但是我不确定它的确切含义。什么是未定义的?
代码的输出可以在http://jsfiddle.net/Bdbtq/找到
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en-us" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="pragma" content="no-cache" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<meta name="robot" content="noindex, nofollow" />
<title>js features</title>
<base href="" />
<link rel="stylesheet" type="text/css" media="all" href="" />
<style type="text/css" media="all">
</style>
</head>
<body>
<div id="container">
<div id="header"></div>
<div id="content">
<p>This is sample content</p>
</div>
<div id="footer">© 2012</div>
</div>
<script type="text/javascript">
function hideMe() {
//hide all div elements
var div = document.getElementsByTagName("div");
for(var i = 0; i < div.length; i = i + 1) {
div.style.display="none";
}
}
</script>
<p onClick="hideMe();">Click to hide</p>
</body>
</html>