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

Detailed explanation of JS event model

小云云
Release: 2018-03-22 16:22:29
Original
1811 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template