在Bootstrap弹出框上显示随机数字

huangapple go评论65阅读模式
英文:

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(&#39;#d100&#39;);
let popoverVal = 0;

popover = new bootstrap.Popover(popoverTriggerEl, {
  content: popoverVal,
  placement: &#39;right&#39;
});

popoverTriggerEl.addEventListener(&#39;show.bs.popover&#39;, () =&gt; {
  popoverVal = Math.random(); // replace with a call to your function

  popover.setContent({
    &#39;.popover-body&#39;: popoverVal.toString()
  });
});

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;button type=&quot;button&quot; class=&quot;btn btn-primary rounded m-5&quot; data-bs-toggle=&quot;popover&quot; id=&quot;d100&quot;&gt;
  Popover Button
&lt;/button&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 04:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定