各位看官帮我解释下这段代码是什么意思?

磨剑重来 2011-12-15 03:42:53

stuHover = function() {
var cssRule;
var newSelector;
for (var i = 0; i < document.styleSheets.length; i++)
for (var x = 0; x < document.styleSheets[i].rules.length ; x++)
{
cssRule = document.styleSheets[i].rules[x];
if (cssRule.selectorText.indexOf("LI:hover") != -1)
{
newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
document.styleSheets[i].addRule(newSelector , cssRule.style.cssText);
}
}
var getElm = document.getElementById("nav").getElementsByTagName("LI");
for (var i=0; i<getElm.length; i++) {
getElm[i].onmouseover=function() {
this.className+=" iehover";
}
getElm[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" iehover\\b"), "");
}
}
}
if (window.attachEvent) window.attachEvent("onload", stuHover);


最好能每句写出下注释。谢谢啦,主要就是cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");和document.styleSheets[i].rules.length 这样的不懂,没接触过。
...全文
188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
磨剑重来 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zsx841021 的回复:]
可以看看这个
[/Quote]
谢谢,不错,对我很有帮助。
三石-gary 2011-12-15
  • 打赏
  • 举报
回复
角斗士 2011-12-15
  • 打赏
  • 举报
回复
cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
这句是用正则匹配,将selectorText里所有 LI:hover 替换成 LI.iehover
document.styleSheets[i].rules.length
这句的意思是统计document里的样式表的长度。
p2227 2011-12-15
  • 打赏
  • 举报
回复
参考
http://topic.csdn.net/u/20111214/21/0affcb2b-e325-427e-b53e-47e286a6c057.html

a:hover是指鼠标放在超连接时候超连接的样式,这里首先是
把css里面的 LI:hover,替换成 "LI.iehover"
并且利用鼠标事件去模仿,最终是得到li鼠标经过和不经过时的两个不同样式

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧