DOM里的this 和event.srcElement区别

先来看一个例子:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
</head>
<body>
     <input id="Button1" type="button" value="button1" οnclick="alert(event.srcElement.value)" />  
     <input id="Button2" type="button" value="button2" οnclick="alert(this.value)" />    
</body>
</html>


结果:输出"button1"和"button2",两者都能获取事件的value

 

把this和event.srcElement都放在调用函数中

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script type="text/javascript">
        function thistest() {
            alert(this.value);
        }
        function srctest() {
            alert(event.srcElement.value);
        }
        
    </script>
</head>
<body>
    <input type="button" value="button1" οnclick="thistest()">
    <input type="button" value="button2" οnclick="srctest()">
</body>
</html>


结果:this.text()输出"undefined" ,srctest()输出"button2",

结论:this是监听当前的事件的对象,而event.srcElement是引发事件的对象.

验证:采用冒泡事件

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
</head>
<body>    
 		<table id="table" οnclick="alert(this.id)">
        <tr id="tr" οnclick="alert(this.id)">
            <td id="td" οnclick="alert(this.id)">猛点击我吧</td>
        </tr>
    </table>
</body>
</html>

依次显示"td","tr","table",证明this是获取当前事件的对象

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
</head>
<body>    
 		<table id="table" οnclick="alert(event.srcElement.id)">
        <tr id="tr" οnclick="alert(event.srcElement.id)">
            <td id="td" οnclick="alert(event.srcElement.id)">猛点击我吧</td>
        </tr>
       </table>
</body>
</html>


全部显示"td",因为srcElement 是获得引发事件的对象




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值