html在不同浏览器器下颜色不同,javascript – html选择不同的背景颜色为每个选项在每个浏览器中正常工作?...

这篇博客介绍了一种适用于所有浏览器,包括IE在内的CSS代码实现,通过JavaScript来改变select元素背景颜色的方法。代码示例展示了如何通过class切换实现白色、红色、黄色和绿色背景,并提供了一个名为colourFunction的JS函数来处理颜色切换和焦点状态。
摘要由CSDN通过智能技术生成

试试这个代码,适用于包括IE在内的各种浏览器:

HTML

This should have a WHITE background

This should have a RED background

This should have a YELLOW background

This should have a GREEN background

CSS

#select1 {width:150px; color:rgba(0, 0, 0, 0);}

#select1:focus, #select1:focus {

color:black;

}

.white {background:white;}

.red {background:red;}

.yellow {background:yellow;}

.green {background:green}

JS

function colourFunction() {

var myselect = document.getElementById("select1"),

colour = myselect.options[myselect.selectedIndex].className;

myselect.className = colour;

myselect.blur(); //This just unselects the select list without having to click

somewhere else on the page

}

HTH 🙂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值