当前位置:网站首页>DOM event binding
DOM event binding
2022-07-25 22:32:00 【Like the rising sun】
List of articles
1. Event binding
DOM Many events are provided for developers to bind , In response to various operations , Enrich page interaction .
Want to trigger an event , You have to give it first DOM Node binding events , Provide event handlers .
2. Directly in HTML Provide events on
This way is to directly inline events in html In the code
<style> .box {
width: 100px; height: 100px; background: green; } </style>
<div class="box" onclick="alert(' I'm click events ')"></div>
Code in html Generate a green area in , Clicking this area will generate a page reminder , The picture is as follows 
By setting HTML Of onclick attribute , You can bind click events , You can write simple JavaScript Code .
But we can see that this method has limitations , Write a lot of JavaScript The code must be unreasonable .
In the past, there was relatively little interaction between web pages , Will be in body Label binding onload event , That is, what you do after the page is loaded .
3. Set event handler properties
Set the binding event through the event handler property , You need to get DOM node .
The code is as follows
<style> .btn {
border: 1px solid #4caf50; font-size: 22px; padding: 8px 12px; color: #4caf50; outline: none; } .btn:active {
background: #eee; } </style>
<button class="btn">
I am a clickable button
</button>
<script> var btn = document.querySelector('.btn'); btn.onclick = function() {
var text = btn.innerText; btn.innerText = text.replace(' I am a clickable button ', ' I knew you had ordered it when I saw it '); }; </script>
Before clicking this button, the result is 
After clicking finish, it's like this 
adopt onclick You can set the click event of the button .
If events need to be cleared , You can reset the property to null.
This way of binding events is still commonly used , But the disadvantage is that the same event has no other auxiliary conditions , Only one event handler can be bound .
4. Summary
Both of these methods can be bound DOM event , But only one event handler can be bound .
边栏推荐
- arcgis开发常用源码
- Recursive case -c
- 【集训DAY12】Minn ratio 【dfs】【最小生成树】
- Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
- ArcGIS中的WKID
- Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
- [training Day11] Calc [mathematics]
- Matrix of C language
- Common source code for ArcGIS development
- Simulated Xiaomi mall head home page
猜你喜欢

【集训DAY15】油漆道路【最小生成树】

Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
![[training day13] travel [violence] [dynamic planning]](/img/ac/dd52771fec20432fd084d8e3cc89e1.png)
[training day13] travel [violence] [dynamic planning]

ArcGIS10.2配置PostgreSQL9.2标准教程

科大讯飞智能办公本Air电纸书阅读器,让我的工作生活更加健康

【集训DAY15】Boring【树形DP】

Builder pattern

【集训DAY15】简单计算【树状数组】【数学】

Multi data source switching

【PMP学习笔记】第1章 PMP体系引论
随机推荐
【集训DAY13】Backpack【动态规划】【贪心】
Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
Data governance under data platform
VIM usage record
[training Day12] x equation [high precision] [mathematics]
分割金条的代价
Advanced database · how to add random data for data that are not in all user data - Dragonfly Q system users without avatars how to add avatar data - elegant grass technology KIR
torchvision
【集训DAY15】Boring【树形DP】
3dslicer importing medical image data
【集训DAY12】X equation 【高精度】【数学】
力扣解法汇总919-完全二叉树插入器
What is the difference between minor GC and full GC?
ECMA 262 12 Lexical Grammer
According to the use and configuration of data permissions in the open source framework
Short circuit effect of logical operators short circuit and short circuit or
ArcGIS10.2配置PostgreSQL9.2标准教程
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
Recursive case -c
Title: give a group of arrays, arranged from large to small and from small to large.