首先,如果你想用里面的内容HTML,你需要在HTML选项设置为true:
$('.danger').popover({ html : true});
那么你有两个选项来设置Popover的内容
- 使用数据内容属性。这是默认选项。
- 使用返回HTML内容的自定义JS函数。
使用数据内容: 你需要躲避HTML内容,这样的事情:
<a class='danger' data-placement='above'
data-content="<div>This is your div content</div>"
title="Title" href='#'>Click</a>
可以手动逃脱HTML或使用功能。我不知道PHP,但在Rails中我们使用* html_safe *。
使用JS功能: 如果你这样做,你有几个选项。我认为最简单的方法就是将你的div内容隐藏在任何你想要的地方,然后编写一个函数将其内容传递给popover。事情是这样的:
$(document).ready(function(){
$('.danger').popover({
html : true,
content: function() {
return $('#popover_content_wrapper').html();
}
});
});
然后你的HTML看起来像这样:
<a class='danger' data-placement='above' title="Popover Title" href='#'>Click</a>
<div id="popover_content_wrapper" style="display: none">
<div>This is your div content</div>
</div>
希望它能帮助! PS:我在使用弹出窗口时没有设置标题属性,所以请记住总是设置标题。
亲爱的@哈维,我开了一个后续问题。 http://stackoverflow.com/questions/11349058/how-can-i-use-div-in-twitters-popover-with-multiple-buttons – trante 2012-07-05 17:01:22
仅供参考,bootstrap有一个名为“hide”的类设置显示:无 – Domenic 2013-04-23 19:48:10
只是一个说明,但这种方法对于IE7来说非常慢,大概是因为html的副本涉及很多DOM操作。根据我们的经验,它在IE7中无法使用,所以您需要另辟蹊径。 – philw 2014-04-14 13:47:48