Home > Article > Web Front-end > How to display a string using JavaScript
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.
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中文网");
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>");
Execution results
##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>Execution result: PHP Chinese websiteThrough 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!