简体   繁体   English

Javascript更新文本而无需刷新页面

[英]Javascript update text without page refresh

Just learning javascript now. 现在就学习javascript。 I want to make a remembrance ribbon where a persons name is added via a form. 我想制作一个纪念丝带,其中通过表格添加了人的名字。 I have it working but just after it happens the page refreshes and it ends up blank. 我有它的工作,但它发生后,页面刷新,它最终空白。 Is there a quick way to achieve this without a page refresh. 有没有一种无需刷新页面即可快速实现的方法。

https://thimbleprojects.org/mrcpower/573757 https://thimbleprojects.org/mrcpower/573757

By default, when you submit a form the page will refresh. 默认情况下,提交表单时,页面将刷新。 So to prevent that you can add this to the top of your submit event handler function. 因此,为防止这种情况,您可以将其添加到submit Event handler函数的顶部。

function changeText(event) {
    event.preventDefault();
}

try this code: 试试这个代码:

<input type="text" name="" id="yourInputId">
<button onclick="submit()">submit</button>
<div id="ribbonId"></div>
<script type="text/javascript">
function submit(){
    var a = document.getElementById('yourInputId').value;
    document.getElementById('ribbonId').innerHTML = a;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM