`

document.write方法

阅读更多
一个最基本的JavaScript命令是document.write。这个命令简单地打印指定的文本内容到页面上。为了逐字打印文本,在打印的文本字符串加上单引号。
document.write('Hello World!');


上面的js代码将会在页面上显示出"Hello World!"
你可以使用document.write 打印变量。输入变量名称不加上引号,如下:

var mytext = "Hello again";
document.write(mytext);


注意:如果变量名称加上引号,将会打印出变量名称(不会打印变量值)。你可以使用“+”符号来连接变量值和文本字符串。
var colour1 = "purple";
var colour2 = "pink";
document.write('<p>colour1: ' + colour1 + '<br>colour2: ' + colour2 + '</p>');


打印结果如下:
引用
colour1: purple
colour2: pink
分享到:
评论

相关推荐

    document.write()在JavaScript中怎样JSP调用

    document.write()在JavaScript中怎样JSP调用

    用javascript地址栏显示源码

    - 使用`document.write('&lt;body&gt;&lt;/body&gt;')`来清除页面上的所有内容,并重新插入一个空的`&lt;body&gt;`标签。 - 这一步是必要的,因为如果直接修改现有文档的内容而不进行清空操作,可能会导致布局错乱或意料之外的结果。...

    各浏览器对document.getElementById等方法的实现差异解析

    - `document.write`和`document.writeln`在IE6/7/8中可以直接调用,而在其他浏览器中会抛出错误。这是因为这两个方法在非初始文档加载期间在其他浏览器中是不可用的。 - `location.reload`和`history.go`同样在IE6/7...

    document.all Firefox不支持

    现代浏览器如Firefox、Chrome、Safari以及Edge等都遵循W3C的标准,推荐使用`document.getElementById`、`document.getElementsByTagName`、`document.querySelector`和`document.querySelectorAll`等方法来选取和...

    document.all[]详解

    document.write("document.all[" + i + "]=" + document.all[i].tagName + " "); } ``` **输出结果**: ``` document.all.length=272 document.all[0]=! document.all[1]=HTML document.all[2]=HEAD ...

    document.write is not allowed in a script tag(解决方案).md

    document.write is not allowed in a script tag(解决方案).md

    document.write is not allowed in a style tag(解决方案).md

    document.write is not allowed in a style tag(解决方案).md

    document.write is not allowed in a link tag(解决方案).md

    document.write is not allowed in a link tag(解决方案).md

    Javascript模仿php中strtotime()函数实现时间字符串转时间戳方法

    document.write(strtotime('now')); document.write(strtotime('next Sunday')); document.write(strtotime('last month')); document.write(strtotime('+1 weeks')); document.write(strtotime('+1 WEEK')); ...

    document属性和方法.txt

    ### Document属性和方法详解 #### 一、Document对象概述 `Document`对象是浏览器中一个非常重要的对象,它表示整个HTML文档。通过`Document`对象,开发者可以访问页面中的所有元素,实现对网页内容的动态操作。下面...

    《JavaScript 程序设计》 基础篇2

    根据提供的文件内容,以下是从标题、描述、标签和部分内容中提取的详细知识点: 【标题】《JavaScript 程序设计》 基础篇2 【描述】本笔记由西风潇潇编写,涵盖了《JavaScript 程序设计》基础篇的学习内容,其中...

    javascript入门教程

    document.write("a&lt;b = "); qq = a; document.writeln(qq); document.write("a&lt;=b = "); qq = a; document.writeln(qq); document.write("a&gt;b = "); qq = a&gt;b; document.writeln(qq); document.write("a&gt...

    网页制作原代码HFJHJ

    if(hour ){document.write("凌晨好!")} else if (hour ){document.write("早上好!")} else if (hour ){document.write("上午好!")} else if (hour ){document.write("中午好!")} else if (hour ){document.write...

    document.open() 与 [removed]()的区别

    在JavaScript中,`document.open()` 和 `document.write()` 是两个常用的DOM操作方法,它们用于动态地更新HTML文档的内容。然而,这两个方法有着显著的区别,理解它们的差异对于编写高效的前端代码至关重要。 首先...

    用一下[removed].rar

    鉴于`document.write`的局限性和潜在问题,现代Web开发倾向于使用其他方法来动态更新页面内容。以下是一些推荐的替代方法: - DOM操作:使用`innerHTML`, `appendChild`, `insertBefore`, `replaceChild`等DOM ...

    练习2.html逻辑题图案

    document.write(1 + '&nbsp;') } document.write(' '); } for (a = 0; a ; a++){ for (i = 6; i &gt; a ; i--){ document.write(1 + '&nbsp;') } document.write(' '); } 1 1 1 1 1...

    05练习.html 逻辑题图案

    document.write(1 + '&nbsp;') } document.write(' '); } for (a = 0; a ; a++){ for (i = 6; i &gt; a ; i--){ document.write(1 + '&nbsp;') } document.write(' '); } 1 1 1 1 1...

    javascript中的document.open()方法使用介绍

    然后,调用document.open()打开一个新的文档流,紧接着通过document.write()方法将变量msg的内容输出到页面中,最后调用document.close()来结束写入操作。 值得注意的是,在实际开发中,document.open()一般会与...

Global site tag (gtag.js) - Google Analytics