首页 > 解决方案 > 3 个按钮上的 Javascript 警报

问题描述

我是java脚本的新手。我有 3 个按钮。

<button type="button" class="btn btn-primary m-1" id="alert">Alert!</button>
<button type="button" class="btn btn-success m-1" id="confirm">Confirm!</button>
<button type="button" class="btn btn-info m-1" id="prompt">Prompt!</button>

我需要在点击它们时弹出一个警报,所以对于第一个“警报”,它会是你好。第二个是确认您是否确定继续。最后一个是提示,只是其中的一个随机名称。

感谢所有的答案!:D

标签: javascripthtml

解决方案


document.getElementById('alert').addEventListener('click', function() {
  alert('Hello World');
});
document.getElementById('confirm').addEventListener('click', function() {
  confirm('Are you sure?');
});
document.getElementById('prompt').addEventListener('click', function() {
  prompt('Enter your name');
});
<button type="button" class="btn btn-primary m-1" id="alert">Alert!</button>
<button type="button" class="btn btn-success m-1" id="confirm">Confirm!</button>
<button type="button" class="btn btn-info m-1" id="prompt">Prompt!</button>


推荐阅读