笔试题之优化代码
2016-09-03 12:08
281 查看
请优化下段的代码
☛ 【优化1——> js 原生实现事件委托】:
☛ 【优化2——> jQuery实现事件委托】:
for(var i = 0; i < document.getElementsByTagName('a').length; i++) { document.getElementsByTagName('a')[i].onmouseover = function(){ this.style.color = 'red'; }; document.getElementsByTagName('a')[i].onmouseout = function(){ this.style.color = ''; }; }
☛ 【优化1——> js 原生实现事件委托】:
var body = document.getElementById('body'); body.addEventListener('mouseover', function(e) { e = e || window.event; var target = e.target || e.srcElement; if (target.nodeName.toLowerCase() == 'a') { target.style.color = 'red'; } }, false); body.addEventListener('mouseout', function(e) { e = e || window.event; var target = e.target || e.srcElement; if (target.nodeName.toLowerCase() == 'a') { target.style.color = ''; } }, false);
☛ 【优化2——> jQuery实现事件委托】:
var $body = $('body'); $body.on('mouseover', 'a', function() { this.style.color = 'red'; }); $body.on('mouseout', 'a', function() { this.style.color = ''; });
相关文章推荐
- 特权级代码段之间的转移---任务内特权级变换转移
- 【转】如何拿到半数面试公司Offer——我的Python求职之路
- 特权级代码段之间的转移---任务内无特权级变换的转移
- MyEclipse+CXF框架开发WebService
- php获取当地时间
- php取字符串的第一个字符
- 初遇C#之HelloWorld程序
- 从建筑的角度来看编程
- MATLAB中reshape函数的用法
- (二)java.util.Scanner的使用
- 有章法的学习c++(2)初识输入输出
- 判断字符串中的括号是否匹配
- Java学习笔记之对象传值和引用总结
- Java———Annotation注解(一)*
- 《深度探索C++对象模型》—Function语意学(The Semantics of Function)
- 学习笔记--特权级代码段之间的转移(二)
- IIS7.5 上 配置 PHP 环境
- 学习笔记--特权级代码段之间的转移(一)
- RxJava初步探路(基础篇)
- Eclipse 智能提示,改字体大小,更改编码,绑定源码等等