本文介绍一种使用JavaScript实现复选框选中时文字背景变色的方法。通过监听点击事件,动态修改相邻文本元素的背景颜色,适用于需要视觉反馈的表单交互场景。
JavaScript实现复选框选中文字变色方案
本文介绍一种利用JavaScript实现复选框选中状态下文字背景变色的技术方案。
该方法在用户选中复选框时,为关联的文字添加背景色,通过简单的逻辑调整,可应用于多种需要视觉反馈的场景。
具体运行效果如下图所示:

在线演示地址如下:
http://demo.jb51.net/js/2015/js-checkbox-cha-font-color-codes/
具体代码如下:
<html>
<head>
<title>选中复选框文字变色</title>
<style>
.checkbox {
background-Color:e-xpression(this.checked?'yellow':'buttonface');
}
</style>
</head>
<script>
function chaCloor(field){
var pig = field.checked;
pig?field.nextSibling.style.backgroundColor="skyblue":field.nextSibling.style.backgroundColor="white";
}
</script>
<body>
<table height=48 width=136>
<tr>
<td><input type="checkbox" onClick="chaCloor(this)"><span>网页特效</span></td>
</tr>
<tr>
<td><input type="checkbox" onClick="chaCloor(this)"><span>源码下载</span></td>
</tr>
<tr>
<td><input type="checkbox" onClick="chaCloor(this)"><span>编程软件</span></td>
</tr>
</table>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。