当前位置:网站首页>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...
})

边栏推荐
- 在 .NET 6 项目中使用 Startup.cs
- In Net 6 project using startup cs
- sigaction的使用
- 2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
- 2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
- How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
- Basic operations of mongodb [add, delete, modify, query]
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
- ffmpeg下载安装教程及介绍
- How to download pytorch? Where can I download pytorch?
猜你喜欢
随机推荐
nodejs基础:浅聊url和querystring模块
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
【刷题篇】 找出第 K 小的数对距离
Mysql Mac版下载安装教程
中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》
Dynamic programming: Longest palindrome substring and subsequence
Captura下载安装及在Captura配置FFmpeg
Shardingsphere dynamic data source
What is pytorch? Is pytorch a software?
ffmpeg下载安装教程及介绍
FileZilla Client下载安装
Arlo's thinking about himself
shardingsphere动态数据源
【DRM】DRM bridge驱动调用流程简单分析
Error in compiled file: error: unmapped character encoding GBK
MongoDB簡介
Ansible introduction [unfinished (semi-finished products)]
2020-01-01t00:00:00.000000z date format conversion
Téléchargement et installation du client Filezilla
js中#号的作用














