document.write()
Can be used to write directly to the HTML output stream. To put it simply, it is to output the content directly in the web page.
First method: The output content is surrounded by "", and the content within the "" is directly output.
<script type="text/javascript">
// The content is enclosed by "", and the content in "" is output directly.
document.write("I love JavaScript!");
</script>
Second Method: Through variables, output content
<script type="text/javascript">
var mystr="hello world!";
//Directly write the variable name and output the content stored in the variable.
document.write(mystr);
</script>
Third method: Output multiple contents, and use +
signs to connect the contents (Concatenation).
<script type="text/javascript">
var mystr="hello";
//Use + sign to connect multiple contents
document.write(mystr+"I love JavaScript");
</script>
Fourth method: Output HTML tags and work, and the tags are enclosed in "".
<script type="text/javascript">
var mystr="hello";
// After outputting hello, output a newline character
document.write(mystr+"<br>");
document.write("JavaScript");
</script>
Regarding the issue of JS output spaces, if you want to achieve output spaces, you can use the special character " " to achieve
<script type="text/javascript">
document.write(" "); //Output spaces
</script>
Task - Exercise - 10
Now let's output two sentences and print some content in the web page!
1. On the 9th line, use document.write() to output the content of the mychar
variable, and output a newline character at the same time.
2. On the 10th line, use document.write
to print a sentence. Use the variables mystr
, mychar
, "love", and output a complete sentence "I love JavaScript!".
Task Code
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>document.write</title>
<script type="text/javascript">
var mystr="I";
var mychar="JavaScript";
</script>
</head>
<body>
</body>
</html>
//= htmlentities($post["body"]); ?>