【发布时间】:2015-11-30 17:15:09
【问题描述】:
我列出了一个包含<option> 项目的列表,每当我尝试使用脚本验证选择了哪个项目时,Chrome 中的控制台都会显示:
未捕获的类型错误:无法读取 null 的属性“值”
我在 HTML 和 JS 中的代码中都看不到有什么问题,一切看起来都正常吗?
这是 HTML:
<select name="part" id="optionFonts">
<option value="default">--- Choose what you want the changes to affect --- </option>
<option value="all"> ALL ELEMENTS </option>
<option value="title-and-headings"> TITLES AND HEADINGS </option>
<option value="headings"> ALL HEADINGS </option>
<option value="allpars"> ALL PARAGRAPHS </option>
<option value="title"> TITLE </option>
<option value="heading1"> FIRST HEADING </option>
<option value="par1"> FIRST PARAGRAPH </option>
<option value="par2"> SECOND PARAGRAPH </option>
<option value="par3"> THIRD PARAGRAPH </option>
<option value="par4"> FOURTH PARAGRAPH </option>
<option value="heading2"> SECOND HEADING </option>
<option value="par5"> FIFTH PARAGRAPH </option>
</select>
在 JS 中
var optionFonts = document.getElementById('optionFonts');
var title = document.getElementById('stitle');
var div1Title = document.getElementById('d1Title');
var div1Par = document.getElementById('d1Par');
var p1 = document.getElementById('p1');
var p2 = document.getElementById('p2');
var p3 = document.getElementById('p3');
var d2Title = document.getElementById('d2Title');
var d2Par = document.getElementById('d2Par');
function changeTextFontToBebas() {
switch(optionFonts.value) {
case 'all':
title.style.fontFamily = "Bebas";
div1Title.style.fontFamily = "Bebas";
div1Par.style.fontFamily = "Bebas";
p1.style.fontFamily = "Bebas";
p2.style.fontFamily = "Bebas";
p3.style.fontFamily = "Bebas";
d2Title.style.fontFamily = "Bebas";
d2Par.style.fontFamily = "Bebas";
break;
}
}
当我按下关于该字体的框(在本例中为 Bebas 字体)时,它应该更改所有内容的字体,当我按下它时,它将运行 function changeTextFontToBebas() 函数,该函数还将验证首先检查的内容,但它只是给出那个错误。
提前致谢
【问题讨论】:
标签: javascript html option