2016-11-06 130 views
1

短時間顯示文本我正在製作一個簡單的網站程序,它將顯示用戶輸入一些文本並點擊按鈕後的用戶文本輸入。爲此,我創建了一個名爲「textWrite()」的函數,該函數在單擊按鈕OK時執行。但是,當我點擊按鈕時,文本會在很短的時間內出現,並在消失後很快出現。(JavaScript)當使用按鈕onclick

這裏是我的代碼:

body { 
 
\t background-image: url(https://upload.wikimedia.org/wikipedia/commons/5/5c/Sava_river_in_Belgrade,_view_from_Kalemegdan_fortress.jpg); 
 
} 
 

 
#inputCharacter { 
 
\t height: 400px; 
 
\t width: 400px; 
 
\t background-color: rgba(0, 60, 200, 0.6); 
 
\t position: relative; 
 
\t left: 560px; 
 
\t top: 80px; 
 
\t border-radius: 5px; 
 
} 
 

 
#inputField { 
 
\t height: 20px; 
 
\t width: 260px; 
 
\t margin-left: 65px; 
 
\t margin-top: 100px; 
 
\t padding: 5px; 
 
} 
 

 
#heading { 
 
\t position: relative; 
 
\t text-align: center; 
 
\t top: 90px; 
 
\t font-size: 24px; 
 
\t font-family: Verdana; 
 
} 
 

 
#submit { 
 
\t height: 30px; 
 
\t width: 60px; 
 
\t margin-left: 170px; 
 
\t margin-top: 10px; 
 
\t background-color: limegreen; 
 
\t border: 3px solid black; 
 
\t font-weight: bold; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>Character Split</title> 
 
\t <link rel="stylesheet" type="text/css" href="inputDesign.css"> 
 
</head> 
 
<body> 
 
\t <form id="inputCharacter"> 
 
\t \t <h2 id="heading">Type some text</h2> 
 
\t \t <input type="text" name="text" id="inputField" autocomplete="off"> <br> 
 
\t \t <button type="submit" id="submit" onclick="textWrite()">OK</button> 
 
\t \t <p id="output"> 
 
\t \t \t <script type="text/javascript"> 
 

 
\t \t function textWrite() { 
 

 
\t \t \t var inputValue = document.getElementById('inputField').value; 
 
\t \t \t document.getElementById('output').innerHTML = inputValue; 
 
\t \t } 
 

 
\t </script> \t 
 
\t \t </p> 
 
\t </form> 
 
</body> 
 
</html>

+0

表單正在提交,所以頁面正在重新裝入。防止表單被提交 – Geeky

回答

1

<button>處於<form>,並內置行動迴應的形式按一下按鈕默認是形式是提交。您的頁面正在重新加載。

使用type=button而不是type=submit<button>