让IE中所有Tag支持hover
来源:互联网 发布:oa办公软件免费版 编辑:程序博客网 时间:2024/05/24 04:17
CSS中的hover伪类(除了a标签外)在IE中没有效果,在网上看到了一篇文章,用到了一个htc文件,结果还真可以了,嘿嘿,看看吧:
<public:component>
<public:attach event="ondocumentready" handler="parseStylesheets" />
<script>
/**
* Whatever:hover - V1.42.060206 - hover & active
* ------------------------------------------------------------
* (c) 2005 - Peter Nederlof
* Peterned - http://www.xs4all.nl/~peterned/
* License - http://creativecommons.org/licenses/LGPL/2.1/
*
* Whatever:hover is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Whatever:hover is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Credits and thanks to:
* Arnoud Berendsen, Martin Reurings, Robert Hanson
*
* howto: body { behavior:url("csshover.htc"); }
* ------------------------------------------------------------
*/
var csshoverReg = /(^|/s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i,
currentSheet, doc = window.document, hoverEvents = [], activators = {
onhover:{on:'onmouseover', off:'onmouseout'},
onactive:{on:'onmousedown', off:'onmouseup'}
}
function parseStylesheets() {
if(!/MSIE (5|6)/.test(navigator.userAgent)) return;
window.attachEvent('onunload', unhookHoverEvents);
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(!csshoverReg.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|active).*$/, '');
var elements = getElementsBySelect(affected);
if(elements.length == 0) return;
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;
hookHoverEvent(node, events.on, function() { node.className += ' ' + className; });
hookHoverEvent(node, events.off, function() { node.className = node.className.replace(new RegExp('//s+'+className, 'g'),''); });
}
function hookHoverEvent(node, type, handler) {
node.attachEvent(type, handler);
hoverEvents[hoverEvents.length] = {
node:node, type:type, handler:handler
};
}
function unhookHoverEvents() {
for(var e,i=0; i<hoverEvents.length; i++) {
e = hoverEvents[i];
e.node.detachEvent(e.type, e.handler);
}
}
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 identify = (//#([a-z0-9_-]+)/i).exec(select);
if(identify) {
var element = doc.getElementById(identify[1]);
return element? [element]:nodes;
}
var classname = (//.([a-z0-9_-]+)/i).exec(select);
var tagName = select.replace(/(/.|/#|/:)[a-z0-9_-]+/i, '');
var classReg = classname? new RegExp('//b' + classname[1] + '//b'):false;
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(classReg && !classReg.test(node.className)) continue;
nodes[nodes.length] = node;
}
}
return nodes;
}
</script>
</public:component>
test.htm
<html>
<head>
<style type="text/css">
body {behavior:url(csshover.htc);}
#cs{
width:500px;
height:300px;
background-color:#0CF;
text-align:center;
line-height:300px;
}
#cs:hover{
background-color:#00F;
}
</style>
</head>
<body>
<div id="cs">
爱上你的微笑--文字超出显示省略号,大家可以复制这段代码进行实验下
</div>
</body>
</html>
或者用另一种方法:
.hovertr{
event:expression(onmouseover = function(){this.style.backgroundColor='#E3FFC9';},onmouseout = function(){this.style.backgroundColor='#FFFFFF'});
}
- 让IE中所有Tag支持hover
- 让IE6 也支持 :hover —— jquery ie-hover 插件
- html5.js 让所有IE支持HTML5
- ie 10 div 对 hover的支持
- 让IE6也支持hover[JS模拟]
- 让IE6支持hover伪类效果
- 让IE 支持 html5
- 让IE支持HTML5
- 让IE支持HTML5
- 让IE支持css3
- 让IE支持H5
- 让IE支持HTML5
- 让ie支持rgb
- 让 IE 支持HTML5
- hover 在 ie 中使用失效问题
- hover 在 ie 中使用失效问题
- 第二章 让Tag支持El表达式
- 让IE支持XML解析
- 4月20日-4月25日 规划
- strncasecmp
- linux inittab详解
- Windows 7被病毒破坏所有的快捷方式失效,怎么解决?
- J2ME Sprite() 切割图片异常
- 让IE中所有Tag支持hover
- eclipse插件安装后的bug
- 系统提示"安装程序检测到另一个程序要求计算机重新启动"
- Mobile phone development
- 【MFC】利用双缓冲技术解决屏闪问题
- 图片为背景的Jpanel 重叠问题
- VC操作INI文件
- Discuz!7.0.0防灌水和垃圾贴有效过滤
- SSL tomcat