javascript - 单击按钮后消息出现在同一个 div 中

标签 javascript html css forms button

这是我的表格:

<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>

当我点击 NOTIFY ME 按钮时,我希望我的表单消失并且我想在该通知 div 中放置一条消息,如下所示:message after click 执行此操作的最佳方法是什么?

最佳答案

你需要明白当你点击按钮时浏览器会刷新页面所以你需要添加一个监听并防止默认。从那里我简单地通过 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>

关于javascript - 单击按钮后消息出现在同一个 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58739142/

相关文章:

html - 如何在 HTML 的标题属性中添加换行符/换行符

html - 如何在 tvOS 自定义应用程序中包含基于 HTML5 的游戏?

javascript - 在 VueJS 中使用按钮水平滚动

javascript - 不可点击可触摸可突出显示

javascript - 微信(WebGL)小游戏无法在设备上运行,但可以在 devtool 中运行

HTML 下拉移动一切

css - Safari 上的内容闪烁

html - 为什么固定元素覆盖 float 元素?

javascript - 将 base64 格式的图像发送到 Web 服务 - 'application/octet-stream' 不是预期的类型 'text/xml; charset=utf-8'

javascript - AngularStrap typeahead ng-options - 标签与模型不同