如何使用Bootstrap 4警报?

时间:2018-11-14 12:59:02

标签: javascript html bootstrap-4

我想使用引导程序样式的警报,而不是默认的JavaScript警报。

如何在以下代码中触发引导警报而不是常规警报?

<button type="button" onclick="alert('my message')">Click Me!</button>
<div class="alert alert-primary" role="alert">
  my alternative message
</div>

我不想只在按下按钮时在屏幕上不断看到div中的消息。

编辑:我正在寻找的答案是:onclick属性的值应该是什么,以便消息仅在单击按钮后才会显示。

2 个答案:

答案 0 :(得分:0)

这是一个更通用的解决方案,您也可以在其中键入所需的消息。

function alert(message) {
  document.getElementById('alert-msg').innerHTML = message; // set message text
  if (document.getElementById('alert').classList.contains('d-none')) document.getElementById('alert').classList.remove('d-none'); // Display alert-box
}

function closeAlert() {
  document.getElementById('alert').classList.add('d-none'); // hide alert-box
  document.getElementById('alert-msg').innerHTML = ''; // reset message
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<button type="button" class="btn btn-sm btn-primary" onclick="alert('my message');">Click Me!</button>
<div class="alert alert-primary clearfix d-none" role="alert" id="alert">
  <span id="alert-msg"></span><button type="button" class="btn btn-sm btn-primary float-right" onclick="closeAlert();">Close</button>
</div>


否则,您可以像在这里一样轻松地使用模式并设置其消息:

function setMessage(message) {
  document.getElementById('alert-message').innerHTML = message;
  // set your Message to the <span> inside your modal.
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" onclick="setMessage('My Message');">
  Click Me!
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <span id="alert-message"></span>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:-1)

这是一个两部分的解决方案。

首先,您需要HTML标记来表示警报(和触发按钮)

<button id="btnShowAlert" type="button">Click Me!</button>
<div id="myAlert" style="display: none;" class="alert alert-primary" role="alert">
  my alternative message
</div>

然后,您需要使用JavaScript(jQuery)来侦听按钮上的单击事件,然后相应地显示警报。

$('#btnShowAlert').on('click', function(){
    $('#myAlert').style('display', 'block');
});

我的问题是...您可能正在寻找Modals吗?