Home > Article > Web Front-end > CSS实现onMouseOver、onMouseOut效果和层套菜单_html/css_WEB-ITnose
<script><br />/**<br />* Pseudos - V1.30.050121 - hover & active<br />* ---------------------------------------------<br />* Peterned - http://www.xs4all.nl/~peterned/<br />* (c) 2005 - Peter Nederlof<br />*<br />* Credits - Arnoud Berendsen<br />* - Martin Reurings<br />* - Robert Hanson<br />*<br />* howt body { behavior:url("csshover.htc"); }<br />* ---------------------------------------------<br />*/<br /><br />var currentSheet, doc = window.document, activators = {<br />onhover:{on:'onmouseover', off:'onmouseout'},<br />onactive:{on:'onmousedown', off:'onmouseup'}<br />}<br /><br />function parseStylesheets() {<br />var sheets = doc.styleSheets, l = sheets.length;<br />for(var i=0; i<l; i++)<br /> parseStylesheet(sheets[i]);<br />}<br />function parseStylesheet(sheet) {<br /> if(sheet.imports) {<br /> try {<br /> var imports = sheet.imports, l = imports.length;<br /> for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);<br /> } catch(securityException){}<br /> }<br /><br /> try {<br /> var rules = (currentSheet = sheet).rules, l = rules.length;<br /> for(var j=0; j<l; j++) parseCSSRule(rules[j]);<br /> } catch(securityException){}<br />}<br /><br />function parseCSSRule(rule) {<br /> var select = rule.selectorText, style = rule.style.cssText;<br /> if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i).test(select) || !style) return;<br /> <br /> var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');<br /> var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);<br /> var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];<br /> var affected = select.replace(/:hover.*$/, '');<br /> var elements = getElementsBySelect(affected);<br /><br /> currentSheet.addRule(newSelect, style);<br /> for(var i=0; i<elements.length; i++)<br /> new HoverElement(elements[i], className, activators[pseudo]);<br />}<br /><br />function HoverElement(node, className, events) {<br />if(!node.hovers) node.hovers = {};<br />if(node.hovers[className]) return;<br />node.hovers[className] = true;<br />node.attachEvent(events.on,<br /> function() { node.className += ' ' + className; });<br />node.attachEvent(events.off,<br /> function() { node.className =<br /> node.className.replace(new RegExp('\\s+'+className, 'g'),''); });<br />}<br /><br />function getElementsBySelect(rule) {<br />var parts, nodes = [doc];<br />parts = rule.split(' ');<br />for(var i=0; i<parts.length; i++) {<br /> nodes = getSelectedNodes(parts[i], nodes);<br />} return nodes;<br />}<br />function getSelectedNodes(select, elements) {<br /> var result, node, nodes = [];<br /> var classname = (/\.([a-z0-9_-]+)/i).exec(select);<br /> var identify = (/\#([a-z0-9_-]+)/i).exec(select);<br /> var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');<br /> for(var i=0; i<elements.length; i++) {<br /> result = tagName? elements[i].all.tags(tagName):elements[i].all;<br /> for(var j=0; j<result.length; j++) {<br /> node = result[j];<br /> if((identify && node.id != identify[1]) || (classname && !(new RegExp('\\b' +<br /> classname[1] + '\\b').exec(node.className)))) continue;<br /> nodes[nodes.length] = node;<br /> }<br /> } return nodes;<br />}<br /></script>
Test.htm
nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
表格1:
OK | yes | no |
haha | fsdfsdf | 测试 |
木野狐 | html/css | javascript |
表格2:
OK | yes | no |
haha | fsdfsdf | 测试 |
木野狐 | html/css | javascript |
menubar.htm
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">