当前位置:网站首页>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 .
边栏推荐
- SMART S7-200 PLC通道自由映射功能块(DO_Map)
- [C syntax] void*
- Internship: writing common tool classes
- 力扣解法汇总919-完全二叉树插入器
- XSS tool beef XSS installation and use
- IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
- 数据平台下的数据治理
- According to the use and configuration of data permissions in the open source framework
- How to call the size of two numbers with a function--- Xiao Tang
- Share two music playing addresses
猜你喜欢

(1) DDL, DML, DQL, DCL and common data types

ThreadLocal 总结(未完待续)

Wechat official account application development (I)
![[training day15] simple calculation [tree array] [mathematics]](/img/20/a5604f666ab02f47929f80c5597f0a.png)
[training day15] simple calculation [tree array] [mathematics]

【PMP学习笔记】第1章 PMP体系引论

About vscode usage+ Solutions to the problem of tab failure
![[C syntax] void*](/img/34/b29b7bbf8eae9f1730352cac1301a4.png)
[C syntax] void*

Share two music playing addresses

关于getchar和scanf的使用示例及注意点

PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
随机推荐
Scratch seamless butt bron filter
Xiaobai programmer's sixth day
Builder pattern
Data quality: the core of data governance
3dslicer importing medical image data
Explore the use of self increasing and self decreasing operators
Usage of in in SQL DQL query
英文术语对应的解释
Xiaobai programmer's fourth day
分割金条的代价
[training Day12] be go! [dynamic programming] [mathematics]
Today, learn about the use of lists, hyperlinks, image tags, and audio and video
Xiaobai programmer day 8
XSS tool beef XSS installation and use
IFLYTEK smart office book air e-book reader makes my work life healthier
[PMP learning notes] Chapter 1 Introduction to PMP System
SQL basic statement DQL select and extract DML insert delete
D3.js 学习
ML-Numpy
谷歌分析UA怎么转最新版GA4最方便