解决ie6 css:hover 背景色不改变的方案

IE6 的 CSS:hover 伪类

众所周知,IE6 不支持 a:hover 以外的 CSS 伪类,解决办法一般有 3 种方案:

使用 JavaScript 事件

直接使用 JavaScript 的 onmouseover/onmouseenter 和 onmouseout/onmouseleave事件进行针对性开发,这大概没什么好详细介绍的。

使用 CSS 表达式

辅以 CSS 表达式中 JScript 同样也有 onmouseover/onmouseenter 和onmouseout/onmouseleave 事件,用以实现 :hover 效果。
注意:其中冒号之前的事件名称可以随意,关键是 expression() 里面的事件。

ul li{
    onmouseout:expression(οnmοuseοut=function(){this.style.backgroundColor=''});
    onmouseover:expression(οnmοuseοver=function(){this.style.backgroundColor='yellow'});
}
ol li{
    onmouseleave:expression(οnmοuseleave=function(){this.style.backgroundColor=''});
    onmouseenter:expression(οnmοuseenter=function(){this.style.backgroundColor='yellow'});
}
不过应该说使用 CSS class 是更好的实践。

table tr{
    onmouseout:expression(οnmοuseοut=function(){try{this.className=this.className.replace(' hover','')}catch(ex){}});
    onmouseover:expression(οnmοuseοver=function(){this.className+=' hover'});
}
table tr.hover td{background:yellow;}
onmouseout 事件处理函数中加了 try/catch 是为了避免 IE5.5 因为不支持 replace()而报脚本错误。当然为了避免这个错误,还是其他的实现方式:

table tr{
    onmouseenter:expression(οnmοuseοut=function(){
        var c=this.className, h=' hover', l=h.length;
        var s=c.indexOf(h);
        this.className=c.substring(0,s)+c.substr(s+l);
    });
    onmouseleave:expression(οnmοuseοver=function(){this.className+=' hover'});
}
table tr.hover td{background:yellow;}

使用 CSS behavior 行为

使用 Whatever:hover

csshover3.htc

<!--[if lte IE 6]>
<style type="text/css">
body{behavior:url("csshover3.htc"); }
</style>
<![endif]-->

延伸阅读

  1. 强大的CSS表达式 -- expression
  2. CSS Expression 的优化

blog:datetime=2010/09/15 19:20:02:tags=IE6,CSS


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值