我试图在一个引导弹窗内显示HTML,但不知为何它不工作。我在这里找到了一些答案,但对我没用。如果我做错了什么,请告诉我。

<script>
  $(function(){
    $('[rel=popover]').popover({ 
      html : true, 
      content: function() {
        return $('#popover_content_wrapper').html();
      }
    });
  });
</script>

<li href="#" id="example" rel="popover" data-content="" data-original-title="A Title"> 
    popover
</li>

<div id="popover_content_wrapper" style="display: none">
    <div>This is your div content</div>
</div>

当前回答

你可以更改文件'ui-bootstrap-tpls-0.11.0.js'中的'template/popover/popover.html' 把“ng-bind”改为“bind-html-unsafe”

它会用html显示所有弹出窗口。 这是不安全的html。仅当您信任html时使用。

其他回答

这是对杰克的精彩回答稍加修改。

下面的代码确保不含HTML内容的简单弹出窗口不受影响。

JavaScript:

$(function(){
    $('[data-toggle=popover]:not([data-popover-content])').popover();
    $('[data-toggle=popover][data-popover-content]').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();
        }
    });
});

在bootstrap 4.6的最新版本中,您可能还需要使用sanitize:false来添加复杂的html。

$('.popover-with-html').popover({ html : true, sanitize : false })

我真的很讨厌把长HTML内的属性,这是我的解决方案,清楚和简单(替换?随你喜欢):

<a class="btn-lg popover-dismiss" data-placement="bottom" data-toggle="popover" title="Help">
    <h2>Some title</h2>
    Some text
</a>

then

var help = $('.popover-dismiss');
help.attr('data-content', help.html()).text(' ? ').popover({trigger: 'hover', html: true});

这是一个老问题,但这是另一种方式,使用jQuery重用弹出窗口,并继续使用原始的引导数据属性,使其更具语义性:

的链接

<a href="#" rel="popover" data-trigger="focus" data-popover-content="#popover">
   Show it!
</a>

显示的自定义内容

<!-- Let's show the Bootstrap nav on the popover-->
<div id="list-popover" class="hide">
    <ul class="nav nav-pills nav-stacked">
        <li><a href="#">Action</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li><a href="#">Separated link</a></li>
    </ul>
</div>

Javascript

$('[rel="popover"]').popover({
    container: 'body',
    html: true,
    content: function () {
        var clone = $($(this).data('popover-content')).clone(true).removeClass('hide');
        return clone;
    }
});

摆弄完整的例子: http://jsfiddle.net/tomsarduy/262w45L5/

我使用了一个弹出在一个列表,我通过HTML给出了一个例子

<a type="button" data-container="body" data-toggle="popover" data-html="true" data-placement="right" data-content='<ul class="nav"><li><a href="#">hola</li><li><a href="#">hola2</li></ul>'>