【问题标题】:Bootstrap 4.3.1 Popover Does Not Display Table Inside Popover ContentBootstrap 4.3.1 弹出框不显示弹出框内容内的表格
【发布时间】:2019-08-17 03:48:34
【问题描述】:

我有一个 Bootstrap 4.3.1 弹出框,内容中嵌入了一个表格。一切都显示了,但表格没有。在下面的代码中,我尝试了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');
  });
<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>

有些人尝试向我展示一个适用于 Bootstrap 3 的 JSFiddle。我拿走了他们的 JSFiddle,只是将 Bootstrap refs 更改为 4,它就坏了。 Bootstrap 3 JSFiddle | Bootstrap 4 JSFiddle

【问题讨论】:

    标签: jquery twitter-bootstrap popover


    【解决方案1】:

    工具提示和弹出框使用内置清理程序来清理接受 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();
        }
      });
    });
    

    【讨论】:

    • 但是为什么它不清理其他 HTML 标签,比如&lt;div&gt;&lt;table&gt; 有什么特别之处?我不必将 &lt;div&gt; 列入白名单即可显示 HTML —— 事实上,我成功地用 DIV 表模仿了该表..
    • 这绝对应该记录在更显眼的地方。只花了我半个小时。
    • 是的,我也花了一个小时。这应该是正确的答案。谢谢。
    • 答案中缺少th,否则完美!
    • 我修改了 Biri 的答案并将&lt;TH&gt; 添加到他的答案中。希望它现在看起来很完整。 Biri,希望你不要介意!
    【解决方案2】:

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

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

    更新:你知道什么? It's documented 现在:

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

    【讨论】:

    • 有趣的是,即使将 sanitize 设置为 false,它也不会显示表格。使用引导程序 4.5.3。将表格 html 标签列入白名单就可以了
    【解决方案3】:

    对于一些 table 如何在 popover 中不起作用,但您可以使用其他标签,例如 ul。我只是用ul 更新我希望它会帮助你。谢谢

    $("[data-toggle=popover]").popover({
        html: true,    
        content: function() { return $('#a02').html(); },  // Also tried directly without function
        title: 'Test',
      }).click(function() {
    	  $(this).popover('show');
      });
    .popover-html ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    
    .popover-html ul li {  
        display: inline-block;
        white-space: nowrap;
        width: 33%;
    }
    
    .popover-html ul li + li {
      padding-left: 5px;
    }  
    <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 class='popover-html'><ul><li>Column 1</li><li>Column 2</li><li>Column 3</li></ul><ul><li>1</li><li>2</li><li>3</li></ul></div>

    另一个选项,您可以在 data-content 属性中添加 HTML。谢谢

    $("[data-toggle=popover]").popover();
    .popover-html ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    
    .popover-html ul li {  
        display: inline-block;
        white-space: nowrap;
        width: 33%;
    }
    
    .popover-html ul li + li {
      padding-left: 5px;
    }  
    <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 tabindex="0"
       class="btn btn-lg btn-primary" 
       role="button" 
       data-html="true" 
       data-toggle="popover" 
       data-trigger="focus" 
       title="Test" 
       data-content="Some text before table<div class='popover-html'><ul><li>Column 1</li><li>Column 2</li><li>Column 3</li></ul><ul><li>1</li><li>2</li><li>3</li></ul></div>">Click Here for Popover</a>

    【讨论】:

      【解决方案4】:

      你可以这样做:

      $(function() {
        $("[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();
          }
        });
      });
      
      
      <!-- Popover #1 -->
      <a href="#" class="btn btn-primary" tabindex="0" data-toggle="popover" data-trigger="focus" data-popover-content="#a1" data-placement="right">Popover Example</a>
      
      
      <!-- Content for Popover #1 -->
      <div id="a1" class="hidden">
        <div class="popover-heading">Title</div>
        <div class="popover-body">
          <table style="width:100%">
            <tr>
              <td>1</td>
              <td>2</td>
              <td>3</td>
            </tr>
            <tr>
              <td>1</td>
              <td>2</td>
              <td>3</td>
            </tr>
            <tr>
              <td>1</td>
              <td>2</td>
              <td>3</td>
            </tr>
          </table>
        </div>
      </div>
      

      Link to Popover Bootstrap 4.0 Documentation 更新: Updated JSFiddle

      【讨论】:

      • 我可以,但为什么我的代码不起作用?它适用于表格以外的所有内容。它也适用于早期版本的 Bootstrap。
      • 另外一件事,为什么表格的格式被关闭了?默认情况下,所有内容都应对齐到行中。
      • @geneb。我已经更新了答案以匹配您想要的。请查看。
      • 不幸的是,我正在运行相同的代码,但它不起作用。如果您查看 CDN reerence,您将 JSFiddle 与 Bootstrap v3.2.0 相关联。但我的是4.3.2。我声称对于 4.3.2,上面的更新代码不起作用。
      • 这对我来说也不适用于 Bootstrap 4.3.1。
      【解决方案5】:
      <div class=".." data-bs-toggle="popover"  data-bs-trigger="hover"  data-bs-html="true" data-bs-animation="false" data-bs-placement="top" data-bs-content=".." title=".."></div>
      

      添加 data-bs-animation="false" 解决了我的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        • 1970-01-01
        • 2011-07-04
        • 2014-08-01
        • 2018-05-22
        • 2021-06-28
        • 1970-01-01
        相关资源
        最近更新 更多