Home > Web Front-end > JS Tutorial > body text

Use JavaScript to dynamically display text

巴扎黑
Release: 2017-09-11 10:32:16
Original
2361 people have browsed it

This article mainly introduces the use of JavaScript to dynamically display text for everyone. It has certain reference value. Interested friends can refer to it.

The example of this article shares with everyone the method of dynamically displaying text with JS. The specific code is for your reference. The specific content is as follows

index.html:


<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8" >
    <title>页面(HTML5)</title>
    <!-- 通过链接的方式使用 CSS -->
    <link rel="stylesheet" href="css/master.css" rel="external nofollow" />
    <script src="js/main.js" charset="utf-8"></script>
  </head>
  <body>
    <!-- HTML5 语义标签 -->
    <header>
      time is long, life is short
    </header>

    <main>
      <aside class="">
        aside
      </aside>
      <article class="">
        <input id="info" placeholder="输入内容">
        <!-- <input type="button" value="添加"> -->
        <button onclick="show()" type="button" name="button">添加</button>
        <h1 id="result">显示</h1>

      </article>
    </main>

    <footer>Copyright (c) 2016 Copyright Holder All Rights Reserved.</footer>

  </body>
</html>
Copy after login

main.js:


// 定义函数
function show() {
  // 获得id为info的input标签的内容
  var a = document.getElementById(&#39;info&#39;).value;
  // 显示,在id为result处动态显示(清除旧的显示新的)
  document.getElementById(&#39;result&#39;).innerText = a;

}
Copy after login

The show function is encapsulated in main.js, a folder dedicated to storing JS script code. function is a scripting language function belonging to JavaScript. . . Function is a keyword in the JavaScript language, which is used when declaring a function.

demo:

But this display does not align the input box, you can add this to the CSS style file master.css:

The above is the detailed content of Use JavaScript to dynamically display text. 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