在Bootstrap popover内部执行JS

Execute JS inside of a Bootstrap popover

本文关键字:执行 JS 内部 popover Bootstrap      更新时间:2023-12-11

我正试图将表单放置在引导程序中,所有的html都很好地呈现在其中,但不执行任何JavaScript(日期选择器)。

这是html代码:

<button type="button" class="btn btn-lg btn-danger" data-html='true' data-toggle="popover" title="Popover title" data-content='
Html Form code
'>Click to toggle popover</button>

JS:

  $('[data-toggle="popover"]').popover()

我建议在属性中使用选择器

html:

<button type="button" class="btn btn-lg btn-danger" data-popover-content="#mypop" data-toggle="popover" data-trigger="focus">Popover Example</button>
<div class="hidden" id="mypop">
  <div class="popover-heading">
    This is the heading
  </div>
  <div class="popover-body">
    This is the body
  </div>
</div>

js:

$("[data-toggle=popover]").popover({
   html : true,
   content: function() {
      var content = $(this).attr("data-popover-content");
      setTimeout(function(){ console.log('execute'); },500);
      return $(content).children(".popover-body").html();
   },
   title: function() {
      var title = $(this).attr("data-popover-content");
      return $(title).children(".popover-heading").html();
   }
});

链接演示:https://jsfiddle.net/xz45cnt4/

在id mypop 中使用class hidden

在引导程序文档中:

消毒:启用或禁用消毒。如果激活了"template","content"answers"title"选项将被清除。

这意味着所有javascript都将被删除。

在bootstrap.js>函数setElementContent(…

   if (this.config.html) {
    if (this.config.sanitize) {
      content = sanitizeHtml(content, this.config.whiteList, this.config.sanitizeFn);
    }
    $element.html(content);
  } else {
    $element.text(content);
  }

默认值为true,因此:

            $(SomeNodeElement).popover({
            html: true,
            sanitize: false,
            content: someElem.innerHMTL //or whatever
        })