当前位置:网站首页>JS what is an event? Event three elements and operation elements
JS what is an event? Event three elements and operation elements
2022-04-23 09:27:00 【Small white egg tart】
Events can be js Detected behavior Trigger -- Response mechanism
1. Every element in the page can generate a trigger js Events , For example, we can generate an event when the user clicks , And then do something . The drop-down list , The mouse shows ; Password hiding and displaying , By clicking on an element .
Three elements of the event ( Click a button , Pop up a dialog box )
1. Event source The event is triggered by who Button
2. Event type How did it happen For example, click to trigger after Press the keyboard ... Mouse click
3. Event handler By function assignment , What to do Pop-up dialog box
The process of executing an event
1. Get the event source
2. The binding event ( Registration events )
3. Add event handler ( Take the method of function assignment )
// Get the event source
var div = document.getElementById('dd');
// Registration events The handler
div.onclick = function() {
console.log(' I was chosen ');
}
Operational elements - Modify element content
element.innerText=' What to modify ', perhaps element.innerHTML=' What to modify '
Content from start to end
1.innerText Don't recognize html label , The label inside will directly show , Nonstandard , At the same time, line breaks and spaces will be removed
2.innerHTML Most used distinguish html label W3C standard , Use more , Keep spaces and wrap
div.innerText = '<strong> It's today </strong>2022';
div.innerHTML = '<strong> It's today </strong>2022';


Operational elements - Modify the attributes of an element
<body>
<!-- src href id alt title
Click different buttons to modify different pictures -->
<button id="ldh"> Lau Andy </button>
<button id="zxy"> Jacky Cheung </button>
<img src="ldh.jpg" alt="" title=" Lau Andy ">
<script>
// Modify element properties src
//1. Get elements Three elements
var ldh = document.getElementById('ldh');
var zxy = document.getElementById('zxy');
var img = document.querySelector('img');
//2. Registration events The handler
zxy.onclick = function() {
img.src = 'zy.jpg';
img.title = ' Jacky Cheung '
}
ldh.onclick = function() {
img.src = 'ldh.jpg';
img.title = ' Lau Andy ';
}
</script>
</body>
Operational elements - Modify form properties
A special , Out-of-service innerHTML and innerText
<body>
<button> Button </button>
<input type="text" value=" Input content ">
<!-- type value checked seleccted disabled -->
<script>
// Get elements
var btn = document.querySelector('button');
var input = document.querySelector('input');
//2. Registration events The handler
btn.onclick = function() {
// input.innerHTML = ' Click. ';
// Don't change inner HTML It's just an ordinary box The value in the form is through value To modify the
input.value = ' By clicking the ';
// btn.disabled = true;
this.disabled = true;
// It points to the caller of the event function btn
}
// disabled Disabled If you want a form to be disabled No more clicks
</script>
</body>
Operational elements - Modify style properties
<script>
var div = document.querySelector('div');
div.onclick = function() {
//div.style The attributes inside take the hump naming method
//js The modified style will be added to the inline style , So the weight is high ,
this.style.backgroundColor = 'purple';
this.style.width = '250px';
}
</script>
Use className Modify style properties
var div = document.querySelector('div');
div.onclick = function() {
// this.style.color = '#666';
// this.style.backgroundColor = 'pink';
// this.style.width = '300px';
// There are too many styles to change. It's troublesome to write It can be used className
div.className = 'first change';
// If you want to keep the previous style Plus Because this directly overwrites the previous class name
}
版权声明
本文为[Small white egg tart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624114143.html
边栏推荐
- JS prototype chain
- MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
- Set the maximum width of the body, but why does the background color of the body cover the whole page?
- 653. 两数之和 IV - 输入 BST
- 成功的DevOps Leader 应该清楚的3个挑战
- Pre parsing of JS
- NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
- Chapter VIII project stakeholder management of information system project manager summary
- Open services in the bottom bar of idea
- Get trustedinstaller permission
猜你喜欢
![[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)](/img/c9/43a63f526068ef6a3e4964a22c5a1f.png)
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)

Go language learning notes - structure | go language from scratch

kettle庖丁解牛第14篇之JSON输入

亚马逊云科技入门资源中心,从0到1轻松上云

Kettle experiment (III)

Canary publishing using ingress

《信息系统项目管理师总结》第八章 项目干系人管理
![[geek challenge 2019] havefun1](/img/8b/b15bf31771d54db25f24d630e64093.png)
[geek challenge 2019] havefun1

Kettle experiment

Flink SQL realizes the integration of stream and batch
随机推荐
Using JS to realize a thousandth bit
Two methods of building Yum source warehouse locally
Number theory to find the sum of factors of a ^ B (A and B are 1e12 levels)
MYCAT configuration
I don't understand time, timestamp and time zone. Look at this article
How to read excel table to database
Go language learning notes - exception handling | go language from scratch
Project upload part
Wechat applet catchtap = "todetail" event problem
Trc20 fund collection solution based on thinkphp5 version
Common errors of VMware building es8
What is monitoring intelligent playback and how to use intelligent playback to query video recording
Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
Single sign on SSO
MySQL小練習(僅適合初學者,非初學者勿進)
JS case to find the maximum value, reverse the array, bubble sort
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
Simple understanding of arguments in JS
Kettle experiment (III)
Two declaration methods of functions of JS