1、首先HTML DOM属性“fontWeight”没有用错。
w3c官方定义:
2、未定义的fontWeight属性,是因为“body.childNodes[1].firstChild”找错子节点:
3、建议修改:<!DOCTYPE html>
<html>
<head>
<title> testing webpage </title>
</head>
<body>
<script>
function boldFirstAndLastNav()
{
document.getElementsByTagName("nav")[0].firstChild.style.fontWeight="bold";
document.getElementsByTagName("nav")[0].lastChild.style.fontWeight="bold";
}
</script>
<nav><a href="http://bbs.fishc.com">Home</a> | <a href="http://bbs.fishc.com">Why?</a> | <a href = "http://bbs.fishc.com">What?</a></nav>
<p>This is a test website.</p>
<script>
boldFirstAndLastNav();
</script>
</body>
</html>
4、涉及dom节点遍历,建议通过chrome最简单的"console.log()"进行排查,下面两段代码自行测试:var b = document.getElementsByTagName("nav");
console.log(b);
var a = document.getElementsByTagName("nav")[0];
console.log(a);
|