英文:
Display random number on Bootstrap popover
问题
我有一个在按钮上的 Bootstrap 弹出框,我希望每次点击时显示一个随机数。然而,我的当前实现只显示第一个生成的数字,不会在后续点击时更新。
这是我的代码。
英文:
I have a bootstrap popover on a button that I would like to display a random number every time its clicked. However my current implementation only displays the first number generated and is not updated on subsequent clicks.
Here's my code.
<button type="button" class="btn btn-primary rounded" data-bs-toggle="popover" id="d100">
Popover Button
</button>
<script>
$(document).ready(function() {
$('#d100').click(function() {
var popoverContent = generalDice(1,100,0);
// Show the popover
$(this).popover({
content: popoverContent,
placement: 'top',
trigger: 'manual'
}).popover('show');
// Remove the popover when clicking outside
$(document).on('click', function(event) {
if (!$(event.target).closest('#d100').length) {
$('#d100').popover('hide').popover('destroy');
}
});
// Prevent click event propagation to avoid immediate removal
return false;
});
});
</script>
答案1
得分: 1
大多数 Bootstrap 组件都有事件可供使用,而不是编写自己的事件处理程序。(点击处理程序已经存在。)您可以在 show
事件上调用您的更新函数,并使用 setContent
方法来更新值。
自从 v5 发布以来,我就没有再使用 jQuery 与 Bootstrap 了,所以如果您更喜欢使用它,您需要进行一些翻译。请参考 youmightnotneedjquery.com 来帮助您逐步摆脱这个越来越不必要的依赖。
const popoverTriggerEl = document.querySelector('#d100');
let popoverVal = 0;
popover = new bootstrap.Popover(popoverTriggerEl, {
content: popoverVal,
placement: 'right'
});
popoverTriggerEl.addEventListener('show.bs.popover', () => {
popoverVal = Math.random(); // 替换为调用您的函数
popover.setContent({
'.popover-body': popoverVal.toString()
});
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<button type="button" class="btn btn-primary rounded m-5" data-bs-toggle="popover" id="d100">
Popover Button
</button>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
英文:
Most Bootstrap components have events to leverage instead of writing all your own. (The click handlers already exist.) You could call your update function on the show
event instead, and use the setContent
method to update the value.
I haven't used jQuery with Bootstrap since v5 was released, so you'll have to do some translation if you prefer that. See youmightnotneedjquery.com for help transitioning away from that increasingly unnecessary dependency.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const popoverTriggerEl = document.querySelector('#d100');
let popoverVal = 0;
popover = new bootstrap.Popover(popoverTriggerEl, {
content: popoverVal,
placement: 'right'
});
popoverTriggerEl.addEventListener('show.bs.popover', () => {
popoverVal = Math.random(); // replace with a call to your function
popover.setContent({
'.popover-body': popoverVal.toString()
});
});
<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<button type="button" class="btn btn-primary rounded m-5" data-bs-toggle="popover" id="d100">
Popover Button
</button>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论