event对象

IE9 FF CHROME

<button type="button" id="button" class="btn" name="button">click</button>
<script type="text/javascript">
var oBtn=document.getElementById("button");
oBtn.onclick=function(event){
    console.log(event.target) // <button id="button" class="btn" name="button" type="button">
    console.log(event.srcElement.className) //btn
}
</script>

IE6-8

<button type="button" id="button" class="btn" name="button">click</button>
<script type="text/javascript">
var oBtn=document.getElementById("button");
oBtn.onclick=function(event){
    var event=window.event;
    console.log(event.srcElement) // <button id="button" class="btn" name="button" type="button">
    console.log(event.srcElement.className) //btn
}
</script>

兼容

<button type="button" id="button" class="btn" name="button">click</button>
<script type="text/javascript">
var oBtn=document.getElementById("button");
oBtn.onclick=function(event){
    var event=event || window.event;
    obj = event.srcElement ? event.srcElement : event.target;
    console.log(obj.id)
}
</script>
    原文作者:daysun
    原文地址: https://segmentfault.com/a/1190000003488418
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞