将 Bootstrap 弹出窗口添加到特定的 div 元素

Max*_*aly 2 html javascript jquery popover twitter-bootstrap

我正在尝试将 bootstrap popover 添加到包含任意内容的 div 元素中(但我想出的方法不起作用):

<div id="popover-target">
   <h3>I need a popover</h3>
      <p>
        This is some content for the section
        that needs to be explained by the popover.
      </p>
</div>
<button class="btn">Show popover</button>

<script>
    $(function(){
        $('.btn').click(function(){
            $('#popover-target').popover({title: 'Title', content: 'Test Content', container: 'body', placement: 'right'});
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

如果我更改$('#popover-target')为,$('.btn')则弹出窗口会正确显示。有任何想法吗?

pal*_*aѕн 5

您刚刚初始化了按钮单击时的弹出窗口,如果您#popover-target在单击按钮后将鼠标悬停在div 上,它应该可以正常工作。如果您想在单击按钮并将鼠标悬停在其上后显示它,您可以使用.popover('show')

$('.btn').click(function() {
  $('#popover-target').popover({
      trigger: 'hover',
      title: 'Title',
      content: 'Test Content',
      placement: 'bottom'
    })
    .popover('show');
});
Run Code Online (Sandbox Code Playgroud)