将JavaScript内容放置在Bootstrap Popover中
作者:互联网
我在将JavaScript放入Popover时遇到问题.我想在单击时将来自Chart.js的图形放在Popover内部.为了便于说明,请使用文档http://www.chartjs.org/docs/latest/中的示例图
到目前为止,我有我的Popover-
<button type="button"
class="btn btn-lg btn-primary"
data-toggle="popover"
title="Good vs. Evil Winrate"
data-placement="bottom"
data-content="Graph coming soon!">
Who's Winning?
</button>
然后用jQuery对其进行初始化-
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
我只是不明白如何将图形放置在Popover中,我尝试过的所有操作都将其破坏到Popover根本无法打开的地步.
解决方法:
您可以直接设置popover options in JavaScript,也可以收听事件.
此处的重要选项是html:true,它允许将HTML放置在弹出窗口内容中,否则显示为字符串.
content
option documentation指出:
Default content value if
data-content
attribute isn’t present.
然后,聆听show.bs.popover事件,我们得到先前放置的< canvas>使用jQuery,并将其作为新Chart.js实例的上下文传递.
$('[data-toggle="popover"]').popover({
html: true,
content: '<canvas id="myChart" width="400" height="400"></canvas>',
}).on('shown.bs.popover', function() {
new Chart($('#myChart'), {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 45],
}]
},
// Configuration options go here
options: {}
});
});
<button type="button" class="btn btn-lg btn-primary" data-toggle="popover" title="Good vs. Evil Winrate" data-placement="bottom">Who's Winning?</button>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script>
标签:chart-js,twitter-bootstrap,javascript,jquery 来源: https://codeday.me/bug/20191111/2018633.html