如果有人愿意帮忙,我需要帮助。我无法得到2 getElementById函数的工作,我是一个初学者,所以我相信这是简单的,但我一直无法解决它。我需要点击文字才能更改字体。任何帮助正确的方向将非常感谢。请检查下面的代码。JavaScript getElementById not Working
<?xml version = "1.0" encoding = "utf-8"?>
<!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">
<head>
<title>Solution Page 486 Exercise 12.7</title>
<style type = "text/css">
.option { color: darkblue }
.graybg { background-color: #aaaaaa }
.whitebg { background-color: #ffffff }
.sans { font-family: sans-serif }
.serif { font-family: serif }
</style>
<script type = "text/javascript">
function bodyClass(color)
{
document.body.className = color;
}
</script>
</head>
<body>
<div id = "main">Click on Options Listed Below to see how they modify this page.<br ><br >
<div>Options:
<div onclick = "bodyClass('graybg');"
class = "option">Gray background</div>
<div onclick = "bodyClass('whitebg');"
class = "option">White background</div>
<div onclick = "document.getElementById(" class
= "option" classname ="sans" ? main?).>Sans-serif text</div>
<div onclick = "document.getElementById(" class
= "option" classname ="serif" ? main?).>Serif text</div></div></div>
</body>
</html>
你的标记都搞砸了。 ''div onclick =“document.getElementById('YOUR_ID_HERE')”....>'它应该是怎么样的 – JohnP
getElementById就是这样,它会通过它的ID获得一个元素,例如document.getElementById(“main” )然后会像上面那样获得id =“main”的div。 – Matt