IE6下不支持除a以外的hover效果的解决办法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IE6下不支持除a以外的hover效果的解决办法相关的知识,希望对你有一定的参考价值。

废话不说,直接上方法!

1.下载csshover.htc文件;

代码如下:

技术分享
<attach event="ondocumentready" handler="parseStylesheets" />
<script language="JScript">
/**
* Pseudos - V1.30.050121 - hover & active
* ---------------------------------------------
* Peterned - http://www.xs4all.nl/~peterned/
* (c) 2005 - Peter Nederlof
*
* Credits - Arnoud Berendsen 
*          - Martin Reurings
*           - Robert Hanson
*
* howto: body { behavior:url("csshover.htc"); }
* ---------------------------------------------
*/

var currentSheet, doc = window.document, activators = {
onhover:{on:‘onmouseover‘, off:‘onmouseout‘},
onactive:{on:‘onmousedown‘, off:‘onmouseup‘}
}

function parseStylesheets() {
//window.alert("hi");
var sheets = doc.styleSheets, l = sheets.length;
for(var i=0; i<l; i++) 
   parseStylesheet(sheets[i]);
}
function parseStylesheet(sheet) {
   if(sheet.imports) {
    try {
     var imports = sheet.imports, l = imports.length;
     for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
    } catch(securityException){}
   }

   try {
    var rules = (currentSheet = sheet).rules, l = rules.length;
    for(var j=0; j<l; j++) parseCSSRule(rules[j]);
   } catch(securityException){}
}

function parseCSSRule(rule) {
   var select = rule.selectorText, style = rule.style.cssText;
   if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i).test(select) || !style) return;
  
   var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, ‘on$1‘);
   var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, ‘.$2‘ + pseudo);
   var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
   var affected = select.replace(/:hover.*$/, ‘‘);
   var elements = getElementsBySelect(affected);

   currentSheet.addRule(newSelect, style);
   for(var i=0; i<elements.length; i++)
    new HoverElement(elements[i], className, activators[pseudo]);
}

function HoverElement(node, className, events) {
if(!node.hovers) node.hovers = {};
if(node.hovers[className]) return;
node.hovers[className] = true;
node.attachEvent(events.on,
   function() { node.className += ‘ ‘ + className; });
node.attachEvent(events.off,
   function() { node.className = 
    node.className.replace(new RegExp(‘\\s+‘+className, ‘g‘),‘‘); });
}

function getElementsBySelect(rule) {
var parts, nodes = [doc];
parts = rule.split(‘ ‘);
for(var i=0; i<parts.length; i++) {
   nodes = getSelectedNodes(parts[i], nodes);
} return nodes;
}
function getSelectedNodes(select, elements) {
   var result, node, nodes = [];
   var classname = (/\.([a-z0-9_-]+)/i).exec(select);
   var identify = (/\#([a-z0-9_-]+)/i).exec(select);
   var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, ‘‘);
   for(var i=0; i<elements.length; i++) {
    result = tagName? elements[i].all.tags(tagName):elements[i].all; 
    for(var j=0; j<result.length; j++) {
     node = result[j];
     if((identify && node.id != identify[1]) || (classname && !(new RegExp(‘\\b‘ +
      classname[1] + ‘\\b‘).exec(node.className)))) continue;
     nodes[nodes.length] = node;
    }
   } return nodes;
}
</script>
View Code

2.在head中写入

body { behavior: url(css/csshover.htc); }/*注意路径*/

3.大功告成!

--------------------------------------------------------------------

PS:

一般对div选择器样式设置:hover ie6浏览器不支持,如li:hover img:hover .abc:hover非a链接标签设置hover其ie6均不支持,对于类似的“p:hover”、“img:hover”、"li:hover"、".abc:hover"、“#header:hover”...IE6不支持hover,鼠标经过不会出现赋予css样式,此方法

,此网页css样式定义如div:hover、li:hover、p:hover、自定义命名css选择名称(.abc:hover)、img:hover,所定义赋予样式IE6均支持了。

以上是关于IE6下不支持除a以外的hover效果的解决办法的主要内容,如果未能解决你的问题,请参考以下文章

HTML+CSS基础之兼容性问题

ie6不支持png图片的解决办法

怎么用js固定某个元素,让它不随页面的滚动而滚动,始终固定在窗口的某个位置?

我用Jquery Png后a:hover背景图依旧不透明!

IE6的兼容性问题及解决办法

鼠标移动DIV后变色