Bootstrap 4.3.1 Popover不显示Popover内容内的表

gen*_* b. 8 jquery popover twitter-bootstrap

我有一个Bootstrap 4.3.1 Popover,其内容嵌入了表格。显示所有内容,但不显示该表。在下面的代码,我都试过的功能content以及直接$('#a02').html()

$("[data-toggle=popover]").popover({
    html: true,    
    content: function() { return $('#a02').html(); },  // Also tried directly without function
    title: 'Test'
  }).click(function() {
	  $(this).popover('show');
  });
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>


<a href="#" data-toggle="popover" data-trigger="focus" data-placement="right">
   Click Here for Popover
</a>

<div id="a02" style="display: none;">
   Some text before table
   <div>
      <table width="100%">
          <thead>
              <th>Column 1</th>
              <th>Column 2</th>
              <th>Column 3</th>
          </thead>
          <tbody>
              <tr>
                  <td>1</td>
                  <td>2</td>
                  <td>3</td>
              </tr>
          </tbody>
      </table>
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)

有人尝试向我展示可以与Bootstrap 3配合使用的JSFiddle。我采用了他们的JSFiddle,只是将Bootstrap引用更改为4,但它坏了。 Bootstrap 3 JSFiddle | Bootstrap 4 JSFiddle

小智 21

工具提示和弹出窗口使用内置的清除程序来清除接受HTML的选项

https://getbootstrap.com/docs/4.3/getting-started/javascript/#sanitizer

尝试这个:

$(function() {
    $.fn.popover.Constructor.Default.whiteList.table = [];
    $.fn.popover.Constructor.Default.whiteList.tr = [];
    $.fn.popover.Constructor.Default.whiteList.td = [];
    $.fn.popover.Constructor.Default.whiteList.th = [];
    $.fn.popover.Constructor.Default.whiteList.div = [];
    $.fn.popover.Constructor.Default.whiteList.tbody = [];
    $.fn.popover.Constructor.Default.whiteList.thead = [];

  $("[data-toggle=popover]").popover({
    html: true,
    content: function() {
      var content = $(this).attr("data-popover-content");
      return $(content).children(".popover-body").html();
    },
    title: function() {
      var title = $(this).attr("data-popover-content");
      return $(title).children(".popover-heading").html();
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 绝对应该在更显着的位置对此进行记录。刚刚花了我半个小时。 (6认同)
  • 是的,我也花了一个小时。这应该是正确的答案。谢谢。 (3认同)
  • 但是为什么它不清理其他 HTML 标签,比如 `&lt;div&gt;`?`&lt;table&gt;` 有什么特别之处?我不必将 `&lt;div&gt;` 列入白名单以显示 HTML —— 事实上,我成功地用 DIV 表模仿了该表。 (2认同)

Woo*_*eru 14

biri 的回答没有任何问题,但只是让您知道(因为它的记录也很糟糕),如果您愿意,您可以停用整个消毒剂。

$(function() {
  $("[data-toggle=popover]").popover({
    sanitize: false,
  });
});
Run Code Online (Sandbox Code Playgroud)

更新:你知道什么?现在已经记录在案

+----------+---------+---------+----------------------------------------+
| Name     | Type    | Default | Description                            |
+----------+---------+---------+----------------------------------------+
| sanitize | boolean | true    | Enable or disable the sanitization. If |
|          |         |         | activated 'template', 'content' and    |
|          |         |         | 'title' options will be sanitized.     |
+----------+---------+---------+----------------------------------------+
Run Code Online (Sandbox Code Playgroud)