javascript - 通过没有值参数但带有文本的javascript更改字体
问题描述
我正在创建动态下拉列表,我想获取选项的名称。因为没有我们不能使用的值
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。
如何将带有文本 b/w 选项 HELLO 的 HELLO h1 标签分别更改 为a HELLO 到 b
解决方案
为达到预期结果,请进行以下更改
1.将onChange更改为onchange(小写)
请参阅此链接中的onchange事件详细信息 - https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onchange
- 在引号中提供颜色值,即 document.querySelector('h1').style.color='red'
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>
推荐阅读
- html - WordPress - 将按钮添加到标题区域
- android - 使用 Angular 制作的 Cordova 应用程序进行 InApp 购买
- android - 如何在应用程序中捕捉“外部”/外部按钮?
- python - 为什么必须使用 __new__ 来初始化一个对象?
- javascript - 如何在单击添加时隐藏最后一次单击(添加)按钮,当用户单击删除按钮然后显示上一个隐藏(添加)按钮
- unity3d - 查找场景 Unity 中使用的资源列表
- spring-boot - 在 Spring Boot 中找不到 Couchbase 配置
- anglesharp - AngleSharp 结果的 html 编码
- symfony - 带有选项解析器的 symfony 验证表单
- amazon-web-services - AWS CodeBuild 中的 GetAuthorizationToken 权限错误