Home  >  Article  >  Web Front-end  >  What are the event types of JavaScript?

What are the event types of JavaScript?

青灯夜游
青灯夜游Original
2021-07-16 16:19:404074browse

The event types of JavaScript are: UI events, focus events, mouse and wheel events, keyboard and text events, compound events, change events, HTML5 events, device events, touch and gesture events, etc.

What are the event types of JavaScript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript Event Types

There are many types of events that may occur in a web browser. Here I will mainly focus on the following commonly used event types:

  • UI events
  • Focus events
  • Mouse and wheel events
  • Keyboard and Text event
  • Composite event
  • Change event
  • HTML5 event
  • Device event
  • Touch and gesture event

Part One: UI Events

The UI in the UI event is (User Interface, User Interface), which is triggered when the user interacts with the elements of the page sauna.

UI events mainly include load, unload, abort, error, select, resize, scroll events.

1.load event

 This event occurs when the page is completely loaded (including all images, js files, css external resources such as files), the load event on the window will be triggered.

 This event is the most commonly used event in JavaScript. For example, we often use window.onload=function(){}; this form, that is, when the page Execute the functions after it is completely loaded.

In addition, I have never known that this event can also be used on other elements, such as image elements, as shown below:

<img src="smile.png" onload="alert(&#39;loaded&#39;)">  

 That is, when the image is fully loaded There will be a pop-up window after coming out. Of course, it can also be implemented using JS, as shown below:

var img=document.getElementById("img");
EventUtil.addHandler(img,"load",function(){
      event=EventUtil.getEvent(event);
      alert(EventUtil.getTarget(event).src);
});

2.unload event

 Obviously, this event is relative to the load event of. Triggered after the document is completely unloaded. The unload time is triggered when the user switches from one page to another. The most common use of this event is to clear references to avoid memory leaks.

 This event also has two ways to specify. One is the JavaScript method, using EventUtil.addHandler(); the other is to add a feature to the body element.

It is worth noting that you must be careful when writing the code in the onload event, because it is triggered after the page is unloaded, so Those objects that exist after the page is loaded may not necessarily exist after onload is triggered!

<body onload="alert(&#39;changed&#39;)">

3.resize event

  When the browser window is resized to a new width or height, the resize event is triggered. This event is triggered on the window. #So the handler can also be specified through the onresize attribute in JS or the body element.  

<body onresize="alert(&#39;changed&#39;)">

  If you write this code, a window will pop up when the size of the browser changes.

4.scroll event

This event will be triggered repeatedly while the document is scrolled, so the code of the event handler should be kept as simple as possible.

Part 2: Focus Events

Focus events are triggered when a page element gains or loses focus. There are mainly the following types:

  • blur is triggered when the element loses focus. This event does not bubble and is supported by all browsers.
  • focus Triggered when an element gains focus. This event does not bubble and is supported by all browsers.
  • focusin Triggered when an element gains focus. This event bubbles, some browsers do not support it.
  • focusout Fires when an element loses focus. This event bubbles, which is not supported by some browsers.

NOTE: Even if blur and focus do not bubble, they can still be heard during the capture phase.

Part 3: Mouse and Wheel Events

Mouse events are the most commonly used events in Web development, because the mouse is the main positioning device.

  • click---用户单击鼠标左键或按下回车键触发
  • dbclick---用户双击鼠标左键触发。
  • mousedown---在用户按下了任意鼠标按钮时触发。
  • mouseenter---在鼠标光标从元素外部首次移动到元素范围内时触发。此事件不冒泡
  • mouseleave---元素上方的光标移动到元素范围之外时触发。不冒泡
  • mousemove---光标在元素的内部不断的移动时触发。
  • mouseover---鼠标指针位于一个元素外部,然后用户将首次移动到另一个元素边界之内时触发。
  • mouseout---用户将光标从一个元素上方移动到另一个元素时触发。   
  • mouseup---在用户释放鼠标按钮时触发。

  注意到:上述所有事件除了mouseenter和mouseleave外都冒泡。

  重要:只有在同一个元素上相继触发mousedown和mouseup事件,才会触发click事件。同样,只有在同一个元素上触发两次click事件,才会触发dbclick事件。

dbclick事件的产生过程如下:

  • mousedown

  • mouseup

  • click

  • mousedown

  • mouseup

  • click

  • dbclick

上面介绍了有关鼠标的事件,下面介绍一些对于鼠标光标的位置:客户区坐标位置、页面坐标位置、屏幕坐标位置

一、客户区坐标位置

  通过客户区坐标可以知道鼠标是在视口中什么位置发生的。

  clientX和clientY分别表示鼠标点击的位置。以body的左上角为原点,向右为X的正方向,向下为Y的正方向。这两个都是event的属性。举例如下:

    <button id="clickMe">点我</button>
    <script>
        var button=document.getElementById("clickMe");
        button.onclick=function(event){
            alert(event.clientY+""+event.clientX);
        };    
     </script>

  当我点击按钮的左上角时,显示为00。效果如下:

 

二.页面坐标位置

  和客户区坐标位置不同,页面坐标位置表示鼠标光标在页面而非视口中的位置。因此坐标是从页面本身而非视口的左边和顶边计算的。如果前面的话不能很好的理解,接着看这里:在页面没有滚动的情况下,页面坐标位置和客户区坐标位置是相同的。

  页面坐标

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>页面坐标位置</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            width: 800px;
            height: 1200px;
            /*我的电脑的视口高度为960px;*/
            background: #ccc;
        }
    </style>
</head>
<body>
    <div></div>
    <button id="button"> 点击我</button>
    <script>
        var button=document.getElementById("button");
        button.onclick=function(){
            alert("pageX为"+event.pageX+"pageY为"+event.pageY);
        };
    </script>
</body>
</html>

在上面的例子中,我把div的高设置为了1200px;而我的浏览器视口高度为960px;所以一定需要滚动我们才能点击按钮,最终得到的结果是:pageX为13pageY为1210。

然而IE8及更早的浏览器是不支持事件对象上的页面坐标的,即不能通过pageX和pageY来获得页面坐标,这时需要使用客户区坐标和滚动信息来计算了。而滚动信息需要使用document.body(混杂模式)、document.documentElement(标准模式)中的scrollLeft和scrollTop属性。举例如下:

<button id="button"> 点击我</button>
    <script>
        var button=document.getElementById("button");
        button.onclick=function(){
            var pageX=event.clientX+(document.body.scrollLeft||document.documentElement.scrollLeft);
            var pageY=event.clientY+(document.body.scrollRight||document.documentElement.scrollRight);
            alert("pageX为"+pageX+"pageY为"+pageY);
        };
    </script>

此例子在IE浏览器下可得到同样结果。

三.屏幕坐标位置

  与前两者又有所不同,鼠标事件发生时,还有一个光标相对于整个电脑屏幕的位置。通过screenX和screenY属性就可以确定鼠标事件发生时鼠标指针相对于整个屏幕的位置。举例如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>页面坐标位置</title>
    <style>
        *{
            margin:0;
            padding:0;
        }

    </style>
</head>
<body>
    <button id="button"> 点击我</button>
    <script>
        var button=document.getElementById("button");
        button.onclick=function(){
            alert("X为:"+event.screenX+"Y为:"+event.screenY);
        };
    </script>
</body>
</html>

最终的结果如下:

显然screenX和screenY是相对于屏幕的左方和上方的。

四.修改键

  当点击某个元素时,如果我们同时按下了ctrl键,那么事件对象的ctrlKey属性值将为true,否则为false,对于alt、shift、meta(windows键盘的windows键、苹果机的Cmd键)的事件属性altKey、shiftKey、metaKey同样如此。下面举例如下:

<button id="button"> 点击我</button>
    <script>
        var button=document.getElementById("button");
        button.onclick=function(){
            var array=new Array();
            if(event.shiftKey){
                array.push("shift");
            }
            if(event.ctrlKey){
                array.push("ctrl");
            }
            if(event.altKey){
                array.push("alt");
            }
            if(event.metaKey){
                array.push("meta");
            }
            alert(array.join(","));
        };
    </script>

这个例子中,我首先创建了一个array数组,接着如果我按下了那几个键,就会存入相应的名称。这里我同时按下了四个键,结果如下:

即最终将数组中的四个值拼接成了字符串显示出来。

五、相关元素

    be83eb51f340b921e483a6b46a36fe5f 点击我65281c5ac262bf6d81768915a4a77ac0
    3f1c4e4b6b16bbbd69b2ee476dc4f83a        var button=document.getElementById("button");
        button.onmouseup=function(){
            alert(event.button);
        };    2cacc6d41bbb37262a98f745aa00fbf0

  当我是用左键产生mousedown事件时,弹出窗口为0,中间键为1,右键为2。

六、鼠标滚轮事件

    3f1c4e4b6b16bbbd69b2ee476dc4f83a
        document.onmousewheel=function(){
            alert(event.wheelDelta);
        };
    2cacc6d41bbb37262a98f745aa00fbf0

当我向下滚动滚轮时,效果如下:

 

 

 

第四部分:键盘和文本事件

该部分主要有下面几种事件:

  • keydown:当用户按下键盘上的任意键时触发。按住不放,会重复触发。

  • keypress:当用户按下键盘上的字符键时触发。按住不放,会重复触发。

  • keyup:当用户释放键盘上的键时触发。

  • textInput:这是唯一的文本事件,用意是将文本显示给用户之前更容易拦截文本。

这几个事件在用户通过文本框输入文本时才最常用到。

键盘事件:

document.addEventListener("keydown",handleKeyDownClick,false);

        function handleKeyDownClick(event) {
            var e = event||window.event||arguments.callee.caller.arguments[0];
            if (e&&e.keyCode == 13) {
                alert("keydown");
            }
        }

【推荐学习:javascript高级教程

The above is the detailed content of What are the event types of JavaScript?. 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