DOM元素在更新后立即消失 [英] DOM element disappearing immediately after update
本文介绍了DOM元素在更新后立即消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下javascript函数,当点击一个按钮(使用onclick()事件)时更新div中的文本
它可以工作,但会立即变回旧文本。
I have the following javascript function that updates a text within a div when a button is clicked (using an onclick() event) It works, but it immediately changes back to the old text.
function func()
{
var text = document.getElementById("text");
text.innerHTML = "Changed";
};
HTML
<body>
<form>
<input type="submit" value="Add Text" onclick="func()"/>
</form>
<div id="text">
Text to Change
</div>
</body>
我错过了什么?我也尝试从函数返回'false',但没有运气。
What am I missing? I also tried returning 'false' from the function but no luck.
推荐答案
您实际上正在提交表单。通过向onclick属性添加返回false:
You are actually submitting the form. Prevent that by adding return false to the onclick attribute:
<input type="submit" value="Add Text" onclick="func(); return false;"/>
这篇关于DOM元素在更新后立即消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文