当前位置:网站首页>Solve the problem that the script tag is written in front of the element node and cannot get the element node
Solve the problem that the script tag is written in front of the element node and cannot get the element node
2022-07-24 01:49:00 【M78_ Domestic 007】
Documents are loaded in the order of the document tree , So get script Element node loaded after script node You can't get it .
Three solutions are proposed .
Solution 1 : Use onload event ,window.onload Trigger when the page is loaded .
Code instructions :
<style>
.box {
width: 100px;
height: 100px;
background-color: brown;
cursor: pointer;
}
</style>
<script>
var box=document.getElementsByClassName("box")
box[0].style.background="green"
</script>
<div class="box"> I am a box</div>here script The label is written in div Block front , Error will be reported when running code , Can't get box Elements
Running results :

You can see box The background of the box has not changed , Now we use onload event :
<style>
.box {
width: 100px;
height: 100px;
background-color: brown;
cursor: pointer;
}
</style>
<script>
window.onload=function(){
var box=document.getElementsByClassName("box")
box[0].style.background="green"
}
</script>Running results :

Solutions two and three : Using asynchronous loading of scripts , Two properties are required async and defer, Because they are used in the same way, they are explained together .
These two attributes are in External import js Use when file , When importing externally js Of the file script When the tag is in front of the element node , There will also be cases where the element node cannot be obtained , It needs to be used async and defer.
demonstration :
Create a js File acquisition box The box is imported from the outside :

The next step is just to html In the document :
<style>
.box {
width: 100px;
height: 100px;
background-color: brown;
cursor: pointer;
}
</style>
<script async src="../js/yibu.js"> // Here is the async Switch to defer It can also succeed
</script>
<div class="box"> I am a box</div>Run code :

It can also be seen that , Pay attention to using sometimes async It may not work , Because its execution mechanism is to load synchronously with the document .
Tell me more about it async and defer:
async: Yes async when , The process of loading and rendering subsequent document elements will follow script.js Load and execute in parallel ( asynchronous ).
defer: Yes defer when , The process of loading subsequent document elements will be the same as script.js The loading is done in parallel ( asynchronous ), however script.js After all elements are parsed ,DOMContentLoaded Before the event triggers .
Illustration :

summary :
From a practical point of view , First, throw all the scripts into </body> It was best practice before , Because it's the only optimization option for old browsers , This method can ensure that all other non script elements can be loaded and parsed with the fastest speed .
边栏推荐
- SCM learning notes 7 -- systick timer (based on Baiwen STM32F103 series tutorials)
- Structure the second operation of the actual combat battalion module
- How to use the directory classification function of the new version of easycvr (v2.5.0)?
- Hcip first day notes
- Exchange 2013 SSL certificate installation document
- OSPF (fourth day notes)
- 中小型医院基础网络解决方案
- Advantages and disadvantages of XML
- Summary of HCIA knowledge points
- 医院无线网络系统设计
猜你喜欢

Hospital generic cabling

How to use the directory classification function of the new version of easycvr (v2.5.0)?

Improvement of DB file sequential read caused by insert

医院网络安全架构

Excel simple macro

Notes - record the solution to the failure of @refreshscope dynamic refresh configuration

1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮

jenkins多任務並發構建

Design of hospital wireless network system

Vessel Segmentation in Retinal Image Based on Retina-GAN
随机推荐
MySQL Basics (operators, sorting and paging, multi table queries, functions)
What is restful
How to solve the problem that the device video cannot be played due to the missing CGO playback callback parameters of easycvr platform?
About rapidssl certificate
Hcip network type, PPP session, data link layer protocol
選址與路徑規劃問題(Lingo,Matlab實現)
Review of HCIA
OSPF (sixth day notes)
Notes - record the solution to the failure of @refreshscope dynamic refresh configuration
How to use the directory classification function of the new version of easycvr (v2.5.0)?
代码阅读方法与最佳实践
Three document usage
Arm architecture and programming 5 -- GCC and makefile (based on Baiwen arm architecture and programming tutorial video)
Exchange 2013 SSL certificate installation document
Vessel Segmentation in Retinal Image Based on Retina-GAN
Basic network solutions for small and medium-sized hospitals
暑假第三周
Number of combinations....
Matplotlib save image to file
The implementation method pathlisttomap can convert the input pathlist into a map type with hierarchical structure