簡體   English   中英

為什么在更改內部HTML后,此HTML / JS會恢復為原始格式?

[英]Why does this HTML/JS revert back to the original after changing the inner HTML?

我開始學習JavaScript,我想知道為什么這不會永久地使“短暫”出現在按鈕之前以及為什么它會在按下按鈕之前恢復到原始HTML頁面?

的index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="mind.js"></script>
</head>
<body>
    <h1 id = "identifier"></h1>
    <form>
        <button value = "button!" onclick="doSomething()"> </button>
    </form>
</body>
</html>

mind.js

function doSomething() {
    document.getElementById("identifier").innerHTML = '<i>Ephemeral</i>';
}

因為您提交的表單正在刷新頁面。 如果您還不想提交,請將return false添加到內聯處理程序。

<button value = "button!" onclick="doSomething(); return false;"> </button>

或者在調用之前添加return ,並將return false添加到函數中。

<button value = "button!" onclick="return doSomething();"> </button>

function doSomething() {
    document.getElementById("identifier").innerHTML = '<i>Ephemeral</i>';
    return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM