Execute JS inside of a Bootstrap popover

时间:2016-04-04 17:31:06

标签: javascript jquery html twitter-bootstrap twitter-bootstrap-3

I am trying to place form inside of a bootstrap, all the html render into it just fine, but doesn't execute any of the JavaScript (date-picker).

This is the html code:

<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()

2 个答案:

答案 0 :(得分:1)

I recommend using selector in attributes

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();
   }
});

link demo: https://jsfiddle.net/xz45cnt4/

use class hidden in id mypop

答案 1 :(得分:0)

在引导文档中:

<块引用>

sanitize:启用或禁用消毒。如果激活“模板”,“内容”和“标题”选项将被清理。

这意味着将删除所有 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
        })