本文實例為大家分享了JS動態顯示文本的具體代碼,供大家參考,具體內容如下
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>
main.js:
// 定義函數function show() {  // 獲得id為info的input標簽的內容  var a = document.getElementById('info').value;  // 顯示,在id為result處動態顯示(清除舊的顯示新的)  document.getElementById('result').innerText = a;}show函數封裝在專門存放JS腳本代碼的文件夾main.js,function是屬于JavaScript的腳本語言函數。。。function是JavaScript語言中的關鍵詞,也就是聲明一個函數時使用的。
demo:


但是這個顯示沒有對齊輸入框,可在CSS樣式文件master.css中添加如此:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答