JS(绑定事件)

事件绑定方式一:属性中绑定
<input type="button" value="+" onclick="fun1()">
<script>
    function fun1(){
        alert(123);
    }
</script>
事件绑定方式一带参数this,this指代本标签
<div class="div1" onclick="fun1(this)">div1</div>
<script>
    function fun1(e){
        alert(e.innerHTML);
    }
</script>
事件绑定方式二:script标签中绑定
<div class="div1">div1</div>
<script>
    var tmp=document.getElementsByClassName("div1")[0];
    tmp.onclick=function(){                 //onclick后不跟括号
        alert(123);
    }
</script>
事件绑定方式二之this
<div class="div1">div1</div>
<script>
    var tmp=document.getElementsByClassName("div1")[0];
    tmp.onclick=function(){
        alert(this.innerHTML);              //this也是指代标签
    }
</script>






猜你喜欢

转载自www.cnblogs.com/gaoyukun/p/9010984.html