当前位置:网站首页>document. Usage of write () - write text - modify style and position control

document. Usage of write () - write text - modify style and position control

2022-07-06 21:10:00 viceen

document.write() Usage of - Write text —— Modify the style 、 position control

1、 Write text

Page loading can write , Without rewriting the page .

grammar :document.write(content)
Parameters
content: Mandatory . character string , It can be a variable or an expression with a string value , What is written often includes HTML Markup language

utilize document.write() To write text

<body>
<h1>Head</h1>
    <script> document.write('<p>hello document</p>'); </script>
<h2>Tail</h2>
</body>

This is h1 and h2 A script is embedded between , Use document.write() To write a p label .

Refresh the page , You can see that the final result is

Head
hello document
Tail

That is, the text is inserted at the position where the script is executed . This is because , The browser parses HTML structure DOM When , If you encounter script Will pause , analysis script And execute , Then continue to parse the rest HTML.( Blocking is going on )

Then check in the browser DOM Structure , Will find script And h2 There is one more p, Browser is finished parsing h1 after , meet script And execute it , here document.write Put a paragraph HTML Write code to the document stream ,script After execution , The browser will parse character string , For newly added p The tag is parsed .

<script>
    var sum_count="Sum:";
    document.write("<span>"+sum_count+"</span>")
</script>

<style>
span {
    
   display:inline-block;
   width: 30px;
}    
</style>
原网站

版权声明
本文为[viceen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061250444613.html