Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:无法设置属性';fontFamily';未定义的_Javascript - Fatal编程技术网

Javascript 未捕获类型错误:无法设置属性';fontFamily';未定义的

Javascript 未捕获类型错误:无法设置属性';fontFamily';未定义的,javascript,Javascript,我在javascript中有一个任务,在单击按钮时更改标记的字体,但我得到了这个错误。我认为我的代码中没有任何错误 <button onclick="myFunction()">Set font</button> <script type = "text/javascript"> function myFunction() { document.getElementsByTagName

我在javascript中有一个任务,在单击按钮时更改标记的字体,但我得到了这个错误。我认为我的代码中没有任何错误

  <button onclick="myFunction()">Set font</button>
    <script type = "text/javascript">
    function myFunction() {
        document.getElementsByTagName("body").style.fontFamily = "Times New Roman, Times, serif";
    }
设置字体
函数myFunction(){
document.getElementsByTagName(“body”).style.fontFamily=“Times New Roman,Times,serif”;
}

document.getElementsByTagName(“body”)不返回单个元素,而是返回一个项目数组

您可以通过按索引访问数组来修复代码:

document.getElementsByTagName("body")[0].style.fontFamily

编辑:正如Ivar在评论中提到的:正文可以通过
文档显式访问。正文

您可以直接使用
文档。正文