使用Twitter的Bootstrap时,如何更改popover的内容?

bre*_*c79 12 javascript popover twitter-bootstrap

我正在使用Twitter的Bootstrap js中的popover功能.我有一个按钮,当单击时,执行此javascript:

$("#popover_anchor").popover({trigger: "manual",
                              placement: "below",
                              offset: 10,
                              html: true,
                              title: function(){return "TITLE";},
                              content: function(){return "CONTENT TEXT";}});
$("#popover_anchor").popover("show");
Run Code Online (Sandbox Code Playgroud)

除了标题和内容函数返回不同的文本之外,还有另一个按钮执行基本相同的javascript.

请注意,它们都在同一元素上定义弹出窗口,只是具有不同的内容.

问题是,一旦点击任一按钮并执行js,其他按钮的后续点击将不会改变弹出窗口内容.因此,一旦弹出了popover,我该如何更新/更改内容?

ani*_*van 17

title属性的目的是接受function提供此功能的类型值.

例如:如果您将标题设置为:

 title: function() { return randomTxt(); }
Run Code Online (Sandbox Code Playgroud)

你有,

function randomTxt()
{
    arr = ['blah blah', 'meh', 'another one'];
    return arr[Math.floor(Math.random() * 4)];
}
Run Code Online (Sandbox Code Playgroud)

你将继续为你的popover获得一个不同的标题.您可以更改randomText动态获取标题的逻辑.


Pau*_*ull 6

你可以直接访问popover实例数据,如下所述:https:
//github.com/twbs/bootstrap/issues/813

此示例取自lnked页面:

var myPopover = $('#element').data('popover')
myPopover.options.someOption = 'foo'
Run Code Online (Sandbox Code Playgroud)