我正在创建动态下拉列表,我想获取选项名称。由于没有值,我们无法使用
document.querySelector('#hello').style.color=this.value;
如此-
<select id="hello">
<option>a</option>
<option>b</option>
</select>
<h1>HELLO</h1>
document.querySelector('#hello').onChange=function(){
document.querySelector('h1').style.fontSize="120px";
document.querySelector('h1').style.color=red;
document.querySelector('h1').innerHTML='1222';
};
我想要的是在将选项a更改为b时更改h1的颜色,大小和innerHTML。
如何使用文本黑白选项更改HELLO h1标签 HELLO to a HELLO to b 分别
答案 0 :(得分:0)
要获得预期结果,请进行以下更改
1。将 onChange 更改为 onchange (小写)
请从此链接-https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onchange
codepen供参考-https://codepen.io/nagasai/pen/PdGVrO?editors=1010
document.querySelector('#hello').onchange=function(){
document.querySelector('h1').style.fontSize="120px";
document.querySelector('h1').style.color='red';
document.querySelector('h1').innerHTML='1222';
};
<select id="hello">
<option>a</option>
<option>b</option>
</select>
<h1>HELLO</h1>