2012-06-17 79 views
50

Twitter Bootstrap的popover功能的标准示例是类似于标题类固醇的工具提示。如何使用Twitter Bootstrap的弹出窗口显示图片?

HTML:

<a href="#" id="blob" class="btn large primary" rel="popover" data-content="And here's some amazing content. It's very engaging. right?" data-original-title="A title">hover for popover</a> 

JS:

<script> 
$("#blob").popover({offset: 10}); 
</script> 

我想使用酥料饼,以显示图像。这可能吗?

+0

请参考此链接。通过使用这个我认为你应该也能够显示图像。 [http://stackoverflow.com/questions/8494291/can-i-use-html-tags-in-twitter-bootstrap-popover- data-content](http://stackoverflow.com/questions/8494291/can html-true-html-tags-in-twitter-bootstrap-popover-data-content) – Vins

回答

84

很简单:)

<a href="#" id="blob" class="btn large primary" rel="popover">hover for popover</a> 

var img = '<img src="https://si0.twimg.com/a/1339639284/images/three_circles/twitter-bird-white-on-blue.png" />'; 

$("#blob").popover({ title: 'Look! A bird!', content: img, html:true }); 

http://jsfiddle.net/weuWk/

+15

为了使html正确显示,需要'html:true' :( – ajreal

+1

添加'trigger:'hover' ' –

6

这是我用过的东西。

$('#foo').popover({ 
    placement : 'bottom', 
    title : 'Title', 
    content : '<div id="popOverBox"><img src="http://i.telegraph.co.uk/multimedia/archive/01515/alGore_1515233c.jpg" /></div>' 
}); 

以及用于在HTML

<b id="foo" rel="popover">text goes here</b> 
14

排序的类似于mattbtay说,但一些变化。需要html:true。
将此脚本放在页面底部朝向贴近身体标记。

<script type="text/javascript"> 
$(document).ready(function() { 
    $("[rel=drevil]").popover({ 
     placement : 'bottom', //placement of the popover. also can use top, bottom, left or right 
     title : '<div style="text-align:center; color:red; text-decoration:underline; font-size:14px;"> Muah ha ha</div>', //this is the top title bar of the popover. add some basic css 
     html: 'true', //needed to show html of course 
     content : '<div id="popOverBox"><img src="http://www.hd-report.com/wp-content/uploads/2008/08/mr-evil.jpg" width="251" height="201" /></div>' //this is the content of the html box. add the image here or anything you want really. 
}); 
}); 
</script> 


然后HTML是:

<a href="#" rel="drevil">mischief</a> 
6

简单与生成的链接:) HTML:

<span class='preview' data-image-url="imageUrl.png" data-container="body" data-toggle="popover" data-placement="top" >preview</span> 

JS:

$('.preview').popover({ 
    'trigger':'hover', 
    'html':true, 
    'content':function(){ 
     return "<img src='"+$(this).data('imageUrl')+"'>"; 
    } 
}); 

http://jsfiddle.net/A4zHC/

1

这里我有一个Bootstrap 3弹出窗口的例子,当鼠标悬停在某些文本上时,它显示一个图像上面有一个图标。我已经加入了一些内联样式,你可能想要取出或更改.....

这也适用于移动设备,因为图像将在第一次点击时弹出,链接将在第二。 HTML:

<h5><a href="#" title="Solid Tiles Template" target="_blank" data-image-url="http://s29.postimg.org/t5pik8lyf/tiles1_preview.jpg" class="preview" rel="popover" style="color: green; font-style: normal; font-weight: bolder; font-size: 16px;">Template Preview 1 <i class="fa fa-external-link"></i></a></h5> 

<h5><a href="#" title="Clear Tiles Template" target="_blank" data-image-url="http://s9.postimg.org/rdonet7jj/tiles2_2_preview.jpg" class="preview" rel="popover" style="color: red; font-style: normal; font-weight: bolder; font-size: 16px;">Template Preview 2 <i class="fa fa-external-link"></i></a></h5> 

<h5><a href="#" title="Clear Tiles Template" target="_blank" data-image-url="http://s27.postimg.org/8scrcdu9v/tiles3_3_preview.jpg" class="preview" rel="popover" style="color: blue; font-style: normal; font-weight: bolder; font-size: 16px;">Template Preview 3 <i class="fa fa-external-link"></i></a></h5> 

JS:

$('.preview').popover({ 
    'trigger':'hover', 
    'html':true, 
    'content':function(){ 
     return "<img src='"+$(this).data('imageUrl')+"'>"; 
    } 
}); 

https://jsfiddle.net/pepsimax_uk/myk38781/3/

+0

您能否详细说明您的答案,并在您提供的解决方案中加入更多描述? – abarisone

+0

当然,您想知道什么? – Pepsimax

+0

好的答案。如果您使用的是AngularJS,那么JavaScript可能不值得包含在控制器初始化回调中,否则它将无法工作。 –

相关问题