如果有人愿意提供帮助,我需要有关此代码的帮助。我无法让 2 个 getElementById 函数正常工作,而且我是初学者,所以我确信它很简单,但我无法修复它。我需要单击文本来更改字体。任何朝着正确方向提供的帮助将不胜感激。请检查下面的代码。
<?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>
最佳答案
getElementById
的语法是:
document.getElementById('the_id_of_an_element')
你有:
document.getElementById(
您需要给它一个参数并包含结束符)
。然后您可能想对返回值执行某些操作。
关于JavaScript getElementById 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8307524/