javascript - 单击按钮后,消息出现在同一个 div 中
问题描述
这是我的表格:
<div class="notification">
<p style="font-family: 'Roboto', sans-serif;">Enter your email to notify you when the app you selected become available.</p>
<form class="form-inline">
<div class="form-group">
<label for="inputemail"></label>
<input type="email" class="form-control" id="inputemail" placeholder="Email Address" size="25">
<button type="submit" class="btn btn-primary" onclick="displayMessage()">NOTIFY ME</button>
<small id="emailHelp" class="form-text text-muted">*we will never sell<br>your email</small>
</div>
</form>
</div>
当我单击通知我按钮时,我希望我的表单消失,并且我想在该通知 div 中放置一条消息,如下所示:单击后 的消息最好的方法是什么?
解决方案
您需要了解,当您单击按钮时,浏览器会刷新页面,因此您需要添加监听并防止默认。从那里我简单地通过 Id 获取 div 元素并更改 innnerHTML 属性。
<script>
let string = 'We will email you when your app becomes available'
function handleClick(event){
event.preventDefault();
let notifDiv = document.getElementById("notifications");
notifDiv.innerHTML = string
}
document.getElementById("notifications").addEventListener("click", handleClick)
</script>
推荐阅读
- syntax - Oracle BI Publisher 程序调用
- algorithm - 有效地计算非常大的数字(如 10**20)的所有完美平方数
- javascript - 如何为谷歌地图 setMarkers 函数中的所有图标变量添加统一大小?
- java - 放心使用 Hamcrest-Date 和 DateTimeFormatter
- asp-classic - 如何使用 Visual Studio 调试经典 ASP
- php - 如何在数组中添加if语句
- javascript - .then() 回调中的“return”是从内部范围还是外部范围返回?
- apache-camel - Camel 3.x:禁用路线的自动注册
- javascript - 在 MySQL 中使用不同的键更新多行
- trusted-timestamp - 可以将粗略时间用作时间戳权限吗?