简体   繁体   English

如何使用 jquery 在 Bootstrap 5 中创建和修改弹出框?

[英]How do you create and modify popover in Bootstrap 5 using jquery?

Does someone know how I can create and add data to bootstrap 5 popover via javascript/jquery?有人知道我如何通过 javascript/jquery 创建和添加数据到 bootstrap 5 popover 吗?

With bootstrap 3 I was able to do this:使用引导程序 3,我能够做到这一点:

$('#element').popover({ placement : 'left', trigger : 'focus', html: true });
let content_template = `<h1>Hello World!</h1>`;
$('#element').data('bs.popover').options.content = content_template;

But I can't figure out how I can do the same with bootstrap 5. The documentation doesn't mention anything about this.但是我不知道如何使用 bootstrap 5 做同样的事情。文档没有提到任何关于这个的内容。 Does anyone know how popovers are managed in BS5?有谁知道如何在 BS5 中管理弹出窗口?

You need to get instance of popover and then use popover_instance._config.content ="some message" to set new content inside your popover .您需要获取popover_instance._config.content ="some message"实例,然后使用popover_instance._config.content ="some message"popover_instance._config.content ="some message"中设置新内容。

Demo Code :演示代码

 new bootstrap.Popover(document.querySelector('[data-bs-toggle]'), { placement: 'left', trigger: 'focus', html: true }) //get instance var popover_instance = bootstrap.Popover.getInstance(document.querySelector('[data-bs-toggle]')) let content_template = `<h1>Hello World!</h1>`; popover_instance._config.content = content_template;//set content
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> <button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" title="Popover title">Click to toggle popover</button>

The popover instance is returned from instantiation. popover 实例是从实例化返回的。 You can then use this to modify the content...然后您可以使用它来修改内容...

const bsPopover = new bootstrap.Popover(document.querySelector('[data-bs-toggle]'), {
  placement: 'left',
  trigger: 'focus',
  html: true
})

bsPopover._config.content = `<h1>Hello World!</h1>`

Demo演示

If you plan on updating popover content multiple times you also need to call setContent() on popover instance.如果您计划多次更新 popover 内容,您还需要在 popover 实例上调用setContent() Like this像这样

popoverInstance._config.content = "Hello world";
popoverInstance.setContent();

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

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