Home >Web Front-end >JS Tutorial >Detailed explanation of JS event model

Detailed explanation of JS event model

小云云
小云云Original
2018-03-22 16:22:291804browse

This article mainly shares with you the detailed explanation of the JS event model, mainly in the form of code, hoping to help everyone.

1. Inline model

<body>
<input  type="button" value="test1" onclick=&#39; alert("dian ji shi jian")&#39;/>
<input type="button" value="test2" onclick="dian()" />
<script>
    function dian(){
        alert("dian ji shi jian")
    }
</script>
</body>

2. Script model

<body>
<input type="button" value="test3" id="dian1"/>
<input type="button" value="test4" id="dian2"/>
<input type="button" value="test5" id="dian3"/>
<script>
    //可通过匿名函数
    var dian1 = document.getElementById(&#39;dian1&#39;);//首先找到这个ID
    dian1.onclick = function(){//根据ID付给它一个点击事件
        alert(&#39;dian ji shi jian&#39;);
    };

    //通过函数赋值
    function dianji(){
        alert(&#39;dian ji shi jian&#39;);
    };
    var dian2 = document.getElementById(&#39;dian2&#39;);
    dian2.onclick=dianji;

    //得到元素的名字
    var dian3 = document.getElementById(&#39;dian3&#39;);
    dian3.onclick = function(){
        alert(this.tagName);
    };

    //删除点击事件
    var dian3 = document.getElementById(&#39;dian3&#39;);
    var count=0;
    dian3.onclick = function(){
        alert(count++);
        if(count==3){
           dian3.onclick = null;
        }
    };
</script>
</body>

Related recommendations:

Detailed explanation of event model

Take you to quickly understand the event model in javascript

Detailed explanation of javascript event model, objects, monitoring, and delivery code examples

The above is the detailed content of Detailed explanation of JS event model. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn