当前位置:网站首页>JS native common knowledge
JS native common knowledge
2022-07-03 03:54:00 【This is not difficult】
Site support :
JavaScript
obtain DOM
grammar :document.querySelector(" Selectors ")document.querySelectorAll(" Selectors ")document.getElementById("id name ")document.getElementsByClassName("class name ")document.getElementsByTagName(" Tag name ")document.getElementsByName("name name ")
Example :
<body>
<div class="app">
<div class="div"> Limitless swordsman </div>
<div id="wuJin"> Endless blade </div>
</div>
</body>
<script>
var hero= document.querySelector(".div");
var equip = document.getElementById("wuJin");
</script>

Properties of the operation element
1. operation class attribute
adopt
classNameoperationclassattribute .
Example :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>className attribute </title>
<style>
div {
width: 800px; height: 600px; }
.bg-red {
background-color: red; }
.bg-blue {
background-color: blue; }
.border {
border: 5px solid skyblue; }
</style>
</head>
<body>
<div class="bg-red"></div>
</body>
<script type="text/javascript">
// 1. obtain DOM object
var div = document.querySelector('div');
// 2. obtain class attribute
console.log(div.className); // bg-red
// 3. modify class attribute
div.className = 'bg-blue';
// 4. add to class attribute
// div.className += ' border';
// Equate to
div.className = 'bg-blue border';
</script>
</html>
2. operation style style
adopt
styleProperty can only get inline css, Cannot get embedded and external chains css.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>style attribute </title>
<style>
input {
background-color: #abcdef; color: #ffffff; }
</style>
</head>
<body>
<input type="button" style="background-color: #0094ff;" value=" Point me ">
</body>
<script type="text/javascript">
// 1. obtain DOM object
var input = document.querySelector('input');
// 2. obtain style attribute
console.log(input.style);
// 3. Get the current background color of the button
console.log(input.style.backgroundColor);
// 4. Set the background color of the button to yellow
input.style.backgroundColor = 'yellow';
input.style.width = '500px';
// 5. Set the text color of the button to red
input.style.color = 'red';
input.style.transform = 'rotate(45deg)';
</script>
</html>
Operate this article and content
innerText
innerHTML

Registration events
grammar :
onclick = function(){}、addEventListener(eventName, handler, useCapture)
addEventListener When binding events, take out the on Prefix
Event name :
1. Mouse events
onclick Click events
<body>
<div class="app">
<div class="div"> Limitless swordsman </div>
<div id="wuJin"> Endless blade </div>
</div>
</body>
<script>
document.querySelector("#wuJin").onclick = () => {
alert(" Need gold coins 3100!");
};
document.getElementsByClassName("hero").addEventListener("click", () => {
alert(" A critical hit !");
});
</script>
ondblclick Double-click the event onmouseenter( Commonly used )、onmouseover Mouse migration onmouseleave( Commonly used )、onmouseout Mouse removal onmousedown The mouse click onmouseup Release the mouse onmousemove Mouse movement onfocus Get focus onblur Lose focus oninput Input event onchange The drop-down box changes onscroll Scroll bar scroll
2. Loading event
onload Loading complete ( And load successfully )
The compiler parses from top to bottom html Of documents , if js The code is written in body front , It may not be available DOM object .
window.onloadThis event will not be triggered until all the contents on the interface are loaded .
<script>
window.onload = function() {
// natural js Code
}
</script>
onbeforeunload Call before the page closes onunload The page is closed
3. Keyboard events
onkeydown Press the key
→textarea Keyboard events for text fields
onkeypress Press the key to trigger when pressing the key with value
onkeypressPress down Ctrl、Atl、Shift… In this way, the key without value will not trigger .
onkeyup The buttons pop up
// Judge whether the pressed is Enter key
document.onkeyup = function(event){
if (event.keyCode != 13) {
return false;
}
}

this Point to
1. In ordinary functions this
Point to window
2. In the constructor this
When used as a normal function ,this Point to window
coordination new When used as a constructor ,this Point to the instantiated object
3. Methods this
Who calls methods ,this Just point to who
4. In the timer this
Point to window

change this Point to
1. call Method
Function name .call(this New point , Actual parameters 1, Actual parameters 2, Actual parameters …)
2. apply Method
Function name .apply(this New point , Array or pseudo array )
3. bind Method
- Method 1
Function name .bind(this New point ) - Method 2
Function name .bind(this New point , Actual parameters 1, Actual parameters 2, Actual parameters …)
var obj = {
name:" Novice hand " };
function test(num1,num2) {
console.log(this);
console.log(num1+num2);
}
test(1,2); // this Point to window
// change this Point to , Make it point to obj
test.call(obj,10,20);
test.apply(obj,[10,20]);
var fn = test.bind(obj);
fn(10,20);
var fn = test.bind(obj,10,20);
fn();
bind Will not execute the function , Still call .

Layout
1. Elastic layout
1.1. Purpose
Let the child elements be displayed on one line .
1.2. Writing position
Elastic properties { display: flex; } Set to parent element .
{ display: flex; }Default Stretch without line breaks .
Line break :flex-warp: warp;
1.3. Spindle direction
The main axis and side axis of the elastic box are perpendicular to each other .
By default :
1.4. Change the spindle direction
grammar
flex-direction: value ;
value :
rowFrom left to rightrow-reverseFrom right to leftcolumnFrom top to bottomcolumn-reverseFrom bottom to top
1.5. Alignment mode
1.5.1. Spindle alignment
namely : The arrangement position of sub elements on the spindle .
grammar
justify-content: value ;
value :
flex-startArrange from the starting position of the spindlecenterArranged in the middle of the spindleflex-endArrange from the end of the spindlespace-betweenThe distance between child elements is the same ( Commonly used )space-aroundThe spacing between the left and right of the child elements is the same ( Commonly used )
1.5.2. Side axis alignment
namely : The vertical arrangement position of the child elements in the whole row .
grammar
align-items: value ;
value :
flex-startLean upcentermiddleflex-endDownstretchThe tensile ( By default, the height of the child element will be stretched to fill the parent element . Premise : The child element has no height .)
1.5.3. Multi line element alignment
grammar
align-content: value ;
value :
flex-startcenterflex-endstretchspace-betweenspace-around
1.6. Elastic division proportion
grammar
Parent element :display: flex;
Subelement :flex: The number ;
meaning : Set the width of the parent element according to flex In proportion to .
If other sub boxes (a With the exception of ) Have a fixed width , Give the remaining width of the parent box to the child box a, So set it directly a Properties offlex: 1;that will do .

Box shadow
box-shadow: 0px 0px 10px 3px #ccc;
paraphrase :
One sided box shadow
Font shadow
timer 、 Timer
timer
effect : The code is executed every once in a while .
establish :
var timeId = setInterval(handler, interval);
timeId : Unique identification of each timer .
handler: Code to execute , It's a function .
interval: Event interval ( millisecond ).
eliminate :
clearInterval(timeId);
eg:
var homeTime;
document.querySelector('.title').onclick = function(){
clearInterval(homeTime);
homeTime = setInterval(function(){
// do something...
},1000)
}
Timer
effect : The code will be executed once every other time , It will close automatically after execution .
establish :
var timeId = setTimeout(handler, interval);
eliminate :
clearTimeout(timeId);
$nextTick
$nextTick It helps us calculate dom The latest update .
It is setTimeout Advanced version of , It is equivalent to helping us calculate the specific time of data rendering to the page .
eg:
this.$nextTick(() => {
// do something...
})

边栏推荐
- For instruction, uploading pictures and display effect optimization of simple wechat applet development
- Download and install captura and configure ffmpeg in captura
- navicat 导出数据库的表结构
- [Yu Yue education] reference materials of political communication science of Communication University of China
- 小程序获取用户头像和昵称
- What is pytorch? Is pytorch a software?
- TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
- IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
- [Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
- 105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
猜你喜欢

毕设-基于SSM宠物领养中心

What can learning pytorch do?

The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units

In Net 6 project using startup cs

在 .NET 6 项目中使用 Startup.cs

Bisher - based on SSM pet adoption center

For instruction, uploading pictures and display effect optimization of simple wechat applet development

Hutool动态添加定时任务
![[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification](/img/30/c840e28c0ef7c8ce574dcde4363863.jpg)
[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification

没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
随机推荐
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
[MySQL] the difference between left join, right join and join
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
2022deepbrainchain biweekly report no. 104 (01.16-02.15)
Appium automated testing framework
Web会话管理安全问题
Commands related to the startup of redis under Linux server (installation and configuration)
Web session management security issues
【刷题篇】接雨水(一维)
[Apple Photo Album push] IMessage group anchor local push
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
[learning notes] seckill - seckill project - (11) project summary
小程序获取用户头像和昵称
【刷题篇】 找出第 K 小的数对距离
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
可分离债券与可转债
递归:深度优先搜索
Bisher - based on SSM pet adoption center





