html <select> onchange无效

时间:2016-05-26 20:26:13

标签: javascript html

我有以下HTML代码:                   &lt; select id =“fontType”&gt;                     &lt; option value =“Arial”style =“font-family:Arial”&gt; Arial&lt; / option&gt;                     &lt; option value =“Courier New”style =“font-family:Courier New”&gt; Courier New&lt; / option&gt;                     &lt; option value =“Georgia”style =“font-family:Georgia”&gt; Georgia&lt; / option&gt;                     &lt; option value =“Tahoma”style =“font-family:Tahoma”&gt; Tahoma&lt; / option&gt;                     &lt; option value =“Times New Roman”style =“font-family:Times New Roman”&gt; Times New Roman&lt; / option&gt;                     &lt; option value =“Trebuchet MS”style =“font-family:Trebuchet MS”&gt; Trebuchet MS&lt; / option&gt;                     &lt; option value =“Verdana”style =“font-family:Verdana”&gt; Verdana&lt; / option&gt;                   &LT; /选择&GT; 我想读出选项中写的内容,但onchange不起作用:                       &LT;脚本&GT;                       document.getElementById(“fontType”)。onchange = function(event){                           警报( “改变了!”);                       };                       &LT; / scrit&GT; 有人能告诉我我做错了什么吗? 非常感谢你, 卢卡斯!

2 个答案:

答案 0 :(得分:1)

没关系,我写了<scrit>而不是<script>

答案 1 :(得分:0)

把它放在窗口onload中,好像你在输出html元素之前声明了js代码。

&#13;
&#13;
<script> 
   window.onload = function() {
     document.getElementById("fontType").onchange = function(event) {
       alert("Changed!");
     };
   }
</script>

<select id="fontType">
  <option value="Arial">Arial</option>
  <option value="Helvetica">Helvetica</option>
</select>
&#13;
&#13;
&#13;