深入浅析JavaScript中对事件的三种监听方式
作者:bea
事件(Event)是JavaScript应用跳动的心脏,也是把所有东西粘在一起的胶水,当我们与浏览器中Web页面进行某些类型的交互时,事件就发生了。 第一种监听方式,也是最普遍使用的方式,是直接在代码上加载事件,产生效果: <table><tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>tex
事件(Event)是JavaScript应用跳动的心脏,也是把所有东西粘在一起的胶水,当我们与浏览器中Web页面进行某些类型的交互时,事件就发生了。
第一种监听方式,也是最普遍使用的方式,是直接在代码上加载事件,产生效果:
<table>
<tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text1</td><td>text2</td></tr>
<tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text3</td><td>text4</td></tr>
<tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text5</td><td>text5</td></tr>
</table>
第二种监听方式,是使用DOM的方式获取对象,并加载事件:
<table>
<tr><td>text1</td><td>text2</td></tr>
<tr><td>text3</td><td>text4</td></tr>
<tr><td>text5</td><td>text5</td></tr>
</table>
<script>
doms = document.getElementsByTagName('tr');
for(i=0;i<doms.length;i++)
{
doms[i].onmouseover = function()
{
this.style.backgroundColor = "red";
}
doms[i].onmouseout = function()
{
this.style.backgroundColor = "";
}
}
</script>
第三种监听方式,是使用标准的addEventListener方式和IE私有的attachEvent方式,因为IE的attachEvent方式在参数传递时的缺陷,这个问题被搞得稍许有些复杂了:
<table>
<tr><td>text1</td><td>text2</td></tr>
<tr><td>text3</td><td>text4</td></tr>
<tr><td>text5</td><td>text5</td></tr>
</table>
<script>
doms = document.getElementsByTagName('tr');
function show_color(where)
{
this.tagName ? where = this : null
where.style.backgroundColor = "red";
}
function hide_color(where)
{
this.tagName ? where = this : null
where.style.backgroundColor = "";
}
function for_ie(where,how)
{
return function()
{
how(where);
}
}
for(i=0;i<doms.length;i++)
{
try
{
doms[i].addEventListener('mouseover',show_color,false);
doms[i].addEventListener('mouseout',hide_color,false);
}
catch(e)
{
doms[i].attachEvent('onmouseover',for_ie(doms[i],show_color));
doms[i].attachEvent('onmouseout',for_ie(doms[i],hide_color));
}
}
</script>
在绑定多个相同的事件的时候,前两种方法会产生覆盖,而第三中方法则会同时执行多个事件。
有用 | 无用
第一种监听方式,也是最普遍使用的方式,是直接在代码上加载事件,产生效果:
<table>
<tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text1</td><td>text2</td></tr>
<tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text3</td><td>text4</td></tr>
<tr onmouseover='this.style.backgroundColor="red"' onmouseout='this.style.backgroundColor=""'><td>text5</td><td>text5</td></tr>
</table>
第二种监听方式,是使用DOM的方式获取对象,并加载事件:
<table>
<tr><td>text1</td><td>text2</td></tr>
<tr><td>text3</td><td>text4</td></tr>
<tr><td>text5</td><td>text5</td></tr>
</table>
<script>
doms = document.getElementsByTagName('tr');
for(i=0;i<doms.length;i++)
{
doms[i].onmouseover = function()
{
this.style.backgroundColor = "red";
}
doms[i].onmouseout = function()
{
this.style.backgroundColor = "";
}
}
</script>
第三种监听方式,是使用标准的addEventListener方式和IE私有的attachEvent方式,因为IE的attachEvent方式在参数传递时的缺陷,这个问题被搞得稍许有些复杂了:
<table>
<tr><td>text1</td><td>text2</td></tr>
<tr><td>text3</td><td>text4</td></tr>
<tr><td>text5</td><td>text5</td></tr>
</table>
<script>
doms = document.getElementsByTagName('tr');
function show_color(where)
{
this.tagName ? where = this : null
where.style.backgroundColor = "red";
}
function hide_color(where)
{
this.tagName ? where = this : null
where.style.backgroundColor = "";
}
function for_ie(where,how)
{
return function()
{
how(where);
}
}
for(i=0;i<doms.length;i++)
{
try
{
doms[i].addEventListener('mouseover',show_color,false);
doms[i].addEventListener('mouseout',hide_color,false);
}
catch(e)
{
doms[i].attachEvent('onmouseover',for_ie(doms[i],show_color));
doms[i].attachEvent('onmouseout',for_ie(doms[i],hide_color));
}
}
</script>
在绑定多个相同的事件的时候,前两种方法会产生覆盖,而第三中方法则会同时执行多个事件。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- Clipboard.js 无需Flash的JavaScript复制粘贴库
- jQuery网页右侧广告跟随滚动代码分享
- jQuery+PHP星级评分实现方法
- 谈谈JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)
- 通过js获取上传的图片信息(临时保存路径,名称,大小)然后通过ajax传递给后端的方法
- 基于OL2实现百度地图ABCD marker的效果
- JS处理json日期格式化问题
- JS日期格式化之javascript Date format
- 详解JavaScript对Date对象的操作问题(生成一个倒数7天的数组)
- RequireJS入门一之实现第一个例子
- 基于jQuery Bar Indicator 插件实现进度条展示效果
- jquery插件pagination实现无刷新ajax分页
- 浅谈Javascript中substr和substring的区别
- jQuery复制表单元素附源码分享效果演示
- js实现创建删除html元素小结
- node.js下LDAP查询实例分享
- 利用jQuery实现漂亮的圆形进度条倒计时插件
- 谈谈JavaScript异步函数发展历程
- JavaScript 对象深入学习总结(经典)