我開始編程。在底層代碼中,運行時會完全刷新頁面。我怎樣才能避免這種情況?非常感謝您的寶貴時間!如何在不刷新頁面的情況下使用javascript在HTML網頁中編寫某些內容?
<h1>Space Calculator Demo</h1><br> If you were to start a trip at
<script type="text/javascript">
function printTime() {
var d = new Date();
var hours = d.getHours();
var mins = d.getMinutes();
var secs = d.getSeconds();
document.body.innerHTML = hours + ":" + mins + ":" + secs;
}
setInterval(printTime, 1000);
</script>
<div id="time"></div> , how long would it take you and how much food and water would you need?
它不會刷新頁面,而是將整個「document.body.innerHTML」的html設置爲文本。 –
它不刷新你的頁面,它只是覆蓋完整的HTML。 'document.body.innerHTML'的選擇器必須是:'document.getElementById('time')。innerHTML',** [示例](https://jsfiddle.net/ak3zwak7/)** – empiric
或[如何在一個元素內更改文本值?](https://stackoverflow.com/questions/15296589/change-element-text-without-jquery),[用jquery替換div中的文本](https:// stackoverflow .COM /問題/ 11867269 /替換,只有文本,裏面-A-DIV-使用-的jQuery) –