Bootstrap 5 – 动态弹出框在隐藏后不再打开

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

Bootstrap 5 - dynamic popover not opening once hidden

问题

I'm trying to open a popover based on the custom selector attribute dynamically using click as trigger using the below code.

var popoverTriggerEl = $('body');
_popover = new bootstrap.Popover(popoverTriggerEl, {
  container: 'body',
  html: true,
  placement: "bottom",
  trigger: 'click',
  selector: '[people-card="click-action"]',
  customClass: 'dpn-peoplecard',
  content: function(event) {
    return 'test';
  }
});

But when I try to close the opened popover on document click using below code, the popovers are getting completely hidden and not opening on the next click.

$(document).click(function(e) {
  if ($(e.target).parent().find('[people-card="click-action"]').length > 0) {
    $('[people-card="click-action"]').popover('hide');
  }
});

I have prepared a JsFiddle based on my requirement where there are two buttons with the same selector. If we try to open the popover by clicking on any button for the first time, the popover is getting opened. But once the document click happens and popover is hidden, we are not able to open any popovers from other selector button clicks.

Note: I'm displaying some HTML content which has some click events in the bootstrap popover content.

JsFiddle Here

Please guide me on the mistake I'm doing here.

英文:

I'm trying to open a popover based on the custom selector attribute dynamically using click as trigger using the below code.

var popoverTriggerEl = $('body');
_popover = new bootstrap.Popover(popoverTriggerEl, {
  container: 'body',
  html: true,
  placement: "bottom",
  trigger: 'click',
  selector: '[people-card="click-action"]',
  customClass: 'dpn-peoplecard',
  content: function(event) {
    return 'test';
  }
});  

But when I try to close the opened popover on document click using below code, the popovers are getting completely hidden and not opening on next click.

$(document).click(function(e) {
  if ($(e.target).parent().find('[people-card="click-action"]').length > 0) {
    $('[people-card="click-action"]').popover('hide');
  }
});  

I have prepared a JsFiddle based on my requirement where there are two buttons with the same selector. If we try to open popover by clicking on any button for the first time, the popover is getting opened. But once the document click happens and popover is hidden, we are not able to open any popovers from other selector button clicks.

Note: I'm displaying some html content which has some click events in the bootstrap popover content.

JsFiddle Here

Please guide me on the mistake I'm doing here

答案1

得分: 1

你应该使用焦点事件(focus event)而不是点击事件(click event)(文档示例):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]');
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl));

<!-- language: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">

<button tabindex="0" type="button" id="button1" class="btn btn-primary" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-content="Dismissible popover" data-bs-placement="bottom">Show Popover</button>

<button tabindex="0" type="button" id="button2" class="btn btn-primary" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-content="Dismissible popover" data-bs-placement="bottom">Show Popover</button>

<!-- end snippet -->

不要回答我要翻译的问题。

英文:

You should use the focus event instead of the click event (doc example) :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const popoverTriggerList = document.querySelectorAll(&#39;[data-bs-toggle=&quot;popover&quot;]&#39;)
const popoverList = [...popoverTriggerList].map(popoverTriggerEl =&gt; new bootstrap.Popover(popoverTriggerEl))

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

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;/&gt;

&lt;button tabindex=&quot;0&quot; type=&quot;button&quot; id=&quot;button1&quot; class=&quot;btn btn-primary&quot; data-bs-toggle=&quot;popover&quot; data-bs-trigger=&quot;focus&quot; data-bs-content=&quot;Dismissible popover&quot; data-bs-placement=&quot;bottom&quot;&gt;Show Popover&lt;/button&gt;

&lt;button tabindex=&quot;0&quot; type=&quot;button&quot; id=&quot;button2&quot; class=&quot;btn btn-primary&quot; data-bs-toggle=&quot;popover&quot; data-bs-trigger=&quot;focus&quot; data-bs-content=&quot;Dismissible popover&quot; data-bs-placement=&quot;bottom&quot;&gt;Show Popover&lt;/button&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定