用CSS控制输入框input悬停交互样式

本文介绍了一种使用CSS表达式实现输入框悬停交互效果的方法,不仅遵循内容与表现分离原则,还减少了XHTML代码量,提高了代码重用性和优化程度。

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

制作表单的时候,实现鼠标悬停交互效果有多种方法:
  1、在xhtml中直接写入onmouseover、onmouseout脚本就可以实现了,但这样就违背了web标准所倡导的内容、表现相分离的原则。以后若要对此进行修改也将会很繁琐。这样直接写入xhtml也会让页面代码增加,如果只是一个input输入框或许是无所谓的,如果是几十个几百个,增加的字节数就很宠大了。
  2、在xhtml中加入小脚本,鼠标经过时可以切换CSS。具体内容请看这篇文章。虽然实现了内容、表现相分离,以后的修改也会很方便。但同样会让页面代码增加。

  我们有没有更好的办法来实现输入框input样式悬停交互的效果呢?
  我们今天就讨论这样的方法,直接在CSS文件中写入悬停交互效果的小脚本。不但实现了内容与表现分离,而且使xhtml代码减小,促进了代码重用、更加的优化。
  这一方法的原理,主要是应用CSS的expression。
input {star : expression(
onmouseover=function(){this.style.borderColor="#99BBE8"},
onmouseout=function(){this.style.borderColor="#B5B8C8"})}
  上面的代码,声明了,当鼠标移上去的时候,边框的颜色是#060,当鼠标移除的时候边框的颜色是#c00。我们看一下运行效果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用CSS控制输入框input悬停交互样式</title>
<style type="text/css">
<!--
input {border:1px solid #B5B8C8;}
input {star : expression(
onmouseover=function(){this.style.borderColor="#99BBE8"},
onmouseout=function(){this.style.borderColor="#B5B8C8"})}
-->
</style>
</head>
<body>
姓名:<input type="text" /><br />
年龄:<input type="text" /><br />
性别:<input type="text" /><br />
手机:<input type="text" /><br />
地址:<input type="text" /><br />
</body>
</html>
你也可以这样做:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用CSS控制输入框input悬停交互样式</title>
<style type="text/css">
<!--
input {border:1px solid #B5B8C8; }
input {star : expression(
onmouseover=function(){this.style.backgroundColor="#DFE8F6"},
onmouseout=function(){this.style.backgroundColor="#FFFFFF"})}
.in40 {width:40px;}
.in80 {width:80px;}
.in120 {width:120px;}
.in200 {width:200px;}
-->
</style>
</head>
<body>
姓名:<input class="in80" type="text" /><br />
年龄:<input class="in40" type="text" /><br />
性别:<input class="in40" type="text" /><br />
手机:<input class="in120" type="text" /><br />
地址:<input class="in200" type="text" /><br />
</body>
</html>

 

 

以下是参考ext样式风格,自定义的显示,该样式触发的条件是文本框获取焦点与否

 

input{
       margin-left:3;
       border:1 solid #B5B8C8;
     font: normal 11px tahoma, arial, helvetica, sans-serif;
     star : expression(
  onfocus=function(){
   this.style.borderColor="#99BBE8";
   this.style.backgroundColor="#DFE8F6";
  },
  onblur=function(){
   this.style.borderColor="#B5B8C8";
   this.style.backgroundColor="#FFFFFF";
   }
    );

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值