简体   繁体   English

为什么在更改内部HTML后,此HTML / JS会恢复为原始格式?

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

I am starting to learn JavaScript and I was wondering why this doesn't permanently make "Ephemeral" appear before the button and why it reverts back to the original HTML page before the button is pressed? 我开始学习JavaScript,我想知道为什么这不会永久地使“短暂”出现在按钮之前以及为什么它会在按下按钮之前恢复到原始HTML页面?

index.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 mind.js

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

Because you're submitting the form, which is refreshing the page. 因为您提交的表单正在刷新页面。 Add return false to the inline handler, if you don't want to submit yet. 如果您还不想提交,请将return false添加到内联处理程序。

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

Or add return before the call, and add return false to the function. 或者在调用之前添加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