input div 等控件变色

本文介绍了一种基于CSS的表单元素变色方案,适用于Firefox、Mozilla、Opera和IE等浏览器,通过定义全局样式和利用:hover事件实现鼠标经过时的背景色和边框颜色变化,增强用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>FireFox / Mozilla / Opera / IE 适用</title>
<style type="text/css" by tesion.>
/* CSS v.02 by tesion. 
CopyRight: http://www.netjoin.net
Contact: renziweb@hotmail.com
原创表单变色
*/
/*定义全局样式 */
input,select,textarea,div	{
	font: 12px Arial
	/* 此部分为表单和容器的字体定义 */
	}
/* 所有表单定义默认 */
input,select,textarea	{
	border: 1px solid #EFEFEF;
	}
/* 利用鼠标事件 :hover 来定义当鼠标经过时样式 */
input:hover,select:hover,textarea:hover	{
	background: #F0F9FB;
	border: 1px solid #1D95C7;
	}
/* 由于 :hover 事件只有 Mozilla 支持,因此为方便IE使用 expression 批量定义 */
input.input,select,textarea {
	tesion:expression(onmouseover=function() 
	{this.style.backgroundColor="#F0F9FB";this.style.border="1px solid #1D95C7"}, 
	onmouseout=function()
	{this.style.backgroundColor="#FFFFFF";this.style.border="1px solid #EFEFEF"})
	}
/* 如上 */
div	{
	background: #EFEFEF;
	padding: 10px; /* 填充 */
	cursor: pointer /* 鼠标 */
	}
div:hover	{
	background: #F0F9FB
	}
div {
	tesion:expression(onmouseover=function() 
	{this.style.backgroundColor="#F0F9FB"}, 
	onmouseout=function()
	{this.style.backgroundColor="#EFEFEF"})
	} 
/* 不错吧? */
</style>
</head>
<body>
<p>
  <input type="text" class="input" size="20" maxlength="16" />
  <input type="text" class="input" size="20" maxlength="16" />
  <input type="text" class="input" size="20" maxlength="16" />
  <input type="text" class="input" size="20" maxlength="16" />
  <input type="text" class="input" size="20" maxlength="16" />
  <input type="text" class="input" size="20" maxlength="16" />
  <input type="text" class="input" size="20" maxlength="16" />
  <input type="button"  value="button" />
  <br />
  <select name="select">
  </select>
  <br />
  <textarea name="textarea"></textarea>
</p>
<div>变色</div>
</body>
</html>

 

### CSS 实现点击后变色效果 通过使用 `:active` 和 `:focus` 伪类,可以轻松实现元素被点击后改变颜色的效果。`:active` 是当用户单击某个元素时触发的状态,而 `:focus` 则是在该元素获取到焦点时生效。 #### 方法一:使用 `:active` 伪类 如果希望在点击时临时改变背景色(松开鼠标后恢复原状),可以通过以下方式实现: ```css .my-demo { background-color: white; } .my-demo:active { background-color: red; /* 点击时变为红色 */ } ``` 上述代码适用于任何可交互的 HTML 元素,例如 `<button>` 或带有 `tabindex` 属性的 `<div>`[^1]。 --- #### 方法二:使用 `:focus` 伪类 对于需要持久化点击状态的情况(即点击后保持新颜色直到失去焦点),可以借助 `:focus` 伪类完成此功能。需要注意的是,默认情况下只有部分标签(如 `<input>`, `<textarea>`, `<button>`)能够自动聚焦。其他标签(如 `<div>`)则需手动为其添加 `tabindex` 属性才能支持 `:focus`。 示例代码如下: ```html <div class="my-focusable" tabindex="0">我可以持续变色</div> ``` ```css .my-focusable { background-color: lightblue; } .my-focusable:focus { background-color: darkblue; /* 获取焦点后变为深蓝色 */ outline: none; /* 移除默认轮廓线 */ } ``` 这里的关键在于为不可聚焦的元素指定 `tabindex` 值。 --- #### 方法三:结合 `checkbox` 的 `:checked` 状态 为了创建一种更复杂的切换机制——比如点击一次改变颜色再点击还原初始状态——推荐采用隐藏型复选框配合其关联容器的方式操作。具体做法如下所示: HTML 结构: ```html <input type="checkbox" id="toggleColor"> <label for="toggleColor" class="color-toggle"></label> ``` 对应样式定义: ```css .color-toggle { display: inline-block; width: 100px; height: 50px; background-color: gray; } #toggleColor:checked + .color-toggle { background-color: greenyellow; /* 当 checkbox 被勾选时应用新的背景色 */ } ``` 这种方法利用了相邻兄弟选择符 (`+`) 来监听复选框的变化并动态调整目标区域外观[^4]。 --- ### 总结 以上三种方法分别针对不同需求提供了灵活解决方案。简单场景下可以直接选用 `:active` 或者增强版带记忆特性的 `:focus`;而对于更加复杂的需求,则建议考虑引入辅助控件如隐式开关按钮来达成预期目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值