简体   繁体   English

动态添加的引导弹出窗口不起作用

[英]Dynamically added bootstrap popover not working

I have found some similar questions, but still could get it to work.我发现了一些类似的问题,但仍然可以让它工作。 So I know that if you add some html content on button click, that jQuery is not listening to it.所以我知道,如果您在单击按钮时添加一些 html 内容,则 jQuery 不会听它。 But I don't know how to fix that in my case.但我不知道如何在我的情况下解决这个问题。 Hope someone can help.希望有人可以提供帮助。 If you click on the first "my popover" it's working.如果您单击第一个“我的弹出框”,它就可以工作。 Also on the red button.也在红色按钮上。 But if you add a new "my popover" with the "click me" button, then the new "my popover" is not working.但是,如果您使用“单击我”按钮添加新的“我的弹出框”,那么新的“我的弹出框”将不起作用。

Edit: And every popover has it's own unique content that is dynamically created.编辑:每个弹出窗口都有自己独特的动态创建的内容。 So I can't have one content for all.所以我不能为所有人提供一个内容。

Here the fiddle jsfiddle这里的小提琴jsfiddle

Here my "button"这是我的“按钮”

<span class="mypopover" data-html="true" data-toggle="popover" title="Title" data-content="Content">
  my popover
</span>

Here my popover js click function这里我的popover js点击function

$(".mypopover").popover({
  trigger: "click",
  sanitize: false,
  html: true,
  animation: true,
  container: "#mycontainer",
});

$(document).on("click", ".mypopover", function (event) {
  event.stopPropagation();
});

$(document).on("click", function () {
  $(".mypopover").popover("hide");
});

When you call $(".mypopover").popover(...) you add popover to all existing elements with class mypopover .当您调用$(".mypopover").popover(...)时,您使用 class mypopover将 popover 添加到所有现有元素。 In order for popover to work on dynamically added elements, you must execute the same code again.为了使 popover 能够处理动态添加的元素,您必须再次执行相同的代码。 But fortunately there's better solution:但幸运的是有更好的解决方案:

You can initialize popover on the body and make use of selector property of popover constructor in the following way:您可以通过以下方式在body上初始化 popover 并使用 popover 构造函数的selector属性:

$("body").popover({
        trigger: "click",
        sanitize: false,
        html: true,
        animation: true,
        selector: '.mypopover',
        container: '#mycontainer',
    });

Here 's the fiddle.是小提琴。

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

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