将Bootstrap popover添加到特定的div元素

时间:2017-06-09 11:38:23

标签: javascript jquery html twitter-bootstrap popover

我正在尝试将一个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>

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

2 个答案:

答案 0 :(得分:3)

如果您在点击按钮后将鼠标悬停在#popover-target div上,则刚刚按下按钮时弹出了弹出窗口,该按钮应该可以正常工作。如果您希望在鼠标悬停在按钮上后显示它,可以使用.popover('show'),如:

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

答案 1 :(得分:0)

&#13;
&#13;
$(function(){
$('.btn').popover({
      container: "body",
      html: true,
      content: function () {
        return '<div class="popover-message">Hello</div>';
      }
    });
    
  
});

$('.btn').click(function(){
      $('#btn').popover();
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


<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>
&#13;
&#13;
&#13;