Home > Web Front-end > JS Tutorial > How to display a string using JavaScript

How to display a string using JavaScript

不言
Release: 2021-04-21 15:24:04
Original
6059 people have browsed it

Methods to use JavaScript to display strings: 1. Use the "document.write" method to display the string; 2. Use the "innerHTML" method to display the string.

How to display a string using JavaScript

The operating environment of this article: Windows 7 system, javascript version 1.8.5, Dell G3 computer.

There are many ways to display strings on web pages. To use JavaScript to display strings, you need to use document.write or innerHTML. Using JavaScript, you can control the display of strings and also display strings containing HTML tags. .

Let’s take a look at two specific methods of using JavaScript to display strings

Methods of using document.write to display strings

To Use document.write to display a string and specify the string to be displayed in the parameter.

The code is as follows

document.write("php中文网");
Copy after login

Execution result: php Chinese website

Through the above method, strings can be displayed.

Display a string containing HTML tags

To display a string containing HTML tags, you need to specify the string to be displayed by including HTML tags in the parameters.

The code is as follows

document.write("<strong>php中文网</strong>");
Copy after login

Execution results

How to display a string using JavaScript

##How to use innerHTML to display strings

To display a string using innerHTML, you need to replace the string specified by id.

<span id="str">php中文网</p>
<script>
var demo2 = document.getElementById("str");
demo2.innerHTML = "PHP中文网";
</script>
Copy after login
Execution result: PHP Chinese website

Through the above method, we can also display strings.

The above is the detailed content of How to display a string using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template