简体   繁体   English

如何使用引导程序 4 警报?

[英]How to use bootstrap 4 alerts?

I would like to use bootstrap's styled alerts instead of the default javascript alerts.我想使用引导程序的样式警报而不是默认的 javascript 警报。

How can I trigger the bootstrap alert instead of the regular alert in the following code?如何在以下代码中触发引导程序警报而不是常规警报?

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

I don't want to see the message inside the div constantly on the screen, only when I press the button.我不想在屏幕上不断地看到 div 内的消息,只有当我按下按钮时。

Edit: the answer that I am looking for is: what should be the value of onclick attribute so that the message will appear ONLY after clicking the button.编辑:我正在寻找的答案是: onclick 属性的值应该是什么,以便仅在单击按钮后才会显示消息。

Here is a more generic Solution for you, where you could type your wanted message, too.这是一个更通用的解决方案,您也可以在其中输入想要的消息。

 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>


Otherwise you could easily use modal and set its message like i did here:否则,您可以轻松地使用模态并像我在这里所做的那样设置其消息:

 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>

this is a 2 part solution.这是一个两部分的解决方案。

First you would need HTML markup that represent the alert (and the trigger button)首先,您需要表示警报(和触发按钮)的 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>

Then secondly you would need the JavaScript (jQuery) that listens for click events on the button, then show the alert accordingly.其次,您需要使用 JavaScript (jQuery) 来侦听按钮上的点击事件,然后相应地显示警报。

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

My question is... Are you perhaps looking for Modals ???我的问题是......你可能在寻找模态吗???

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

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