当前位置:网站首页>document.write()的用法-写入文本——修改样式、位置控制
document.write()的用法-写入文本——修改样式、位置控制
2022-07-06 12:51:00 【viceen】
document.write()的用法-写入文本——修改样式、位置控制
1、写入文本
页面加载中可以写入,而不会出现重写页面问题。
语法:document.write(content)
参数:
content:必选。字符串,可以是变量或值为字符串的表达式,写入的内容常常包括HTML标记语言
利用 document.write()来写入文本
<body>
<h1>Head</h1>
<script> document.write('<p>hello document</p>'); </script>
<h2>Tail</h2>
</body>
这是在h1和h2之间内嵌一个脚本,使用document.write()来写入一个p标签。
刷新页面,可以看到最终的结果是
Head
hello document
Tail
即文本在脚本执行的位置被插入。这是因为,浏览器就解析HTML构建DOM的时候,如果遇到script就会暂停下来,解析script中的代码并执行,然后再继续解析剩余HTML。(阻塞进行的)
此时再去浏览器中检测DOM的结构,会发现script与h2之间多了一个p,浏览器在解析完h1之后,碰到script并执行之,此时document.write将一段HTML代码写入到文档流中,script执行完毕后,浏览器会解析文档流中的字符串,对新添加的p标签进行解析。
<script>
var sum_count="Sum:";
document.write("<span>"+sum_count+"</span>")
</script>
<style>
span {
display:inline-block;
width: 30px;
}
</style>
边栏推荐
- Manifest of SAP ui5 framework json
- [weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
- 2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
- [DIY]如何制作一款个性的收音机
- How to upgrade high value-added links in the textile and clothing industry? APS to help
- 正则表达式收集
- 动态切换数据源
- C language operators
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- What is the problem with the SQL group by statement
猜你喜欢

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件

使用.Net分析.Net达人挑战赛参与情况

Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache

I've seen many tutorials, but I still can't write a program well. How can I break it?

OLED屏幕的使用

SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍

性能测试过程和计划
![[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100

全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎

OneNote 深度评测:使用资源、插件、模版
随机推荐
基于STM32单片机设计的红外测温仪(带人脸检测)
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
OSPF多区域配置
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
请问sql group by 语句问题
Simple continuous viewing PTA
What is the difference between procedural SQL and C language in defining variables
"Penalty kick" games
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Quel genre de programmation les enfants apprennent - ils?
Value of APS application in food industry
(工作记录)2020年3月11日至2021年3月15日
PHP saves session data to MySQL database
7. Data permission annotation
Review questions of anatomy and physiology · VIII blood system
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission