Learn more about the most commonly used JavaScript events

Learn more about the most commonly used JavaScript events

JavaScript events:

An event refers to the execution of certain code that is triggered when certain components perform certain operations.

Commonly used events:

property Trigger timing
onabort Image loading interrupted
onblur The element loses focus
onchange User changes the content of a field
onclick Click the mouse on an object
ondblclick Double-click an object
onerror An error occurred while loading the document or image
onfocus The element has focus
onkeydown A keyboard key is pressed
onkeypress A keyboard key is pressed or held down
onkeyup A keyboard key was released
onload A page or image has finished loading
onmousedown A mouse button is pressed
onmousemove The mouse is moved
onmouseout Move the mouse away from an element
onmouseover The mouse is moved over an element
onmouseup A mouse button is released
onreset The reset button was clicked
onresize The window or frame is resized
onselect Text is selected
onsubmit The submit button is clicked
onunload User logout page

Event Actions

Binding Events

Method 1 : Bind through the event attribute in the tag.

<body>
<img id="img" src="upload/2022/web/u=1582373193,2567665585&fm=26&gp=0.jpg">
<br/>
<button id="up" onclick="up()">Previous</button>&nbsp;
<button id="down" onclick="down()">Next</button>
</body>

<script>
    // Display the first function up() {
        let img = document.getElementById("img");
        img.setAttribute("src", "upload/2022/web/u=1582373193,2567665585&fm=26&gp=0.jpg")
    }
    // Display the second image function down() {
        let img = document.getElementById("img");
        img.setAttribute("src", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.58cdn.com.cn%2Fp1%2Fbig%2Fn_v26f22be8b05a74c42b8f9dfb859480186.jpg%3Fw%3D425%26h%3D320&refer=http%3A%2F%2Fpic4.58cdn.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618451421&t=0310ddbd6f7cd840299b10dd314572c8")
    }
</script>

Method 2 : Binding through DOM element attributes.

<body>

<img id="img" src="upload/2022/web/u=1582373193,2567665585&fm=26&gp=0.jpg">
<br/>
<button id="up">Previous</button>&nbsp;
<button id="down">Next</button>
</body>

<script>
    // Display the first function up() {
        let img = document.getElementById("img");
        img.setAttribute("src", "upload/2022/web/u=1582373193,2567665585&fm=26&gp=0.jpg")
    }

    let s = document.getElementById("up");
    s.onclick = up;

    // Display the second image function down() {
        let img = document.getElementById("img");
        img.setAttribute("src", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.58cdn.com.cn%2Fp1%2Fbig%2Fn_v26f22be8b05a74c42b8f9dfb859480186.jpg%3Fw%3D425%26h%3D320&refer=http%3A%2F%2Fpic4.58cdn.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618451421&t=0310ddbd6f7cd840299b10dd314572c8")
    }

    let x = document.getElementById("down");
    x.onclick = down;
</script>

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Analysis of JavaScript's event loop mechanism
  • JavaScript event capture bubbling and capture details
  • Detailed explanation of js event delegation
  • A brief analysis of event bubbling and event capture in js
  • JavaScript event loop case study
  • Detailed explanation of Javascript event capture and bubbling methods

<<:  Solution to inconsistent display of cursor size in input box

>>:  How to package the docker image, push it to the remote server and deploy it to k8s

Recommend

Solution to the problem of installing MySQL compressed version zip

There was a problem when installing the compresse...

Navicat cannot create function solution sharing

The first time I wrote a MySQL FUNCTION, I kept g...

Vue realizes dynamic progress bar effect

This article example shares the specific code of ...

Understanding innerHTML

<br />Related articles: innerHTML HTML DOM i...

How to use Tencent slider verification code in Vue3+Vue-cli4 project

Introduction: Compared with traditional image ver...

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. G...

Videojs+swiper realizes Taobao product details carousel

This article shares the specific code of videojs+...

Vue implements a scroll bar style

At first, I wanted to modify the browser scroll b...

MySQL 5.7.19 installation and configuration method graphic tutorial (win10)

Detailed tutorial on downloading and installing M...

MySQL master-slave data is inconsistent, prompt: Slave_SQL_Running: No solution

This article uses an example to describe the solu...

How many pixels should a web page be designed in?

Many web designers are confused about the width of...

Implementation of master-slave replication in docker compose deployment

Table of contents Configuration parsing Service C...

Detailed tutorial on installing JDK1.8 on Linux

1. Cleaning before installation rpm -qa | grep jd...

MySQL 5.7.17 installation and configuration tutorial for Mac

1. Download MySQL Click on the official website d...