koi*_*ose 9 ajax jquery popover twitter-bootstrap
我在使用ajax重新加载bootstrap popover的内容时遇到问题.这是一些代码:http://pastie.org/3960102
第二个ajax请求(当我点击"a.close"时)返回一个更新的内容(我可以在控制台中看到它),但它没有加载到popover中.
我四处寻找解决方案,但似乎都没有.
我还能尝试什么?谢谢
data-content
您可以直接访问popover工具提示内容,而不是重置属性.
替换以下行:
t.attr('data-content', r);
Run Code Online (Sandbox Code Playgroud)
使用此工作代码:
t.data('popover').tip().html(r);
Run Code Online (Sandbox Code Playgroud)
2012年更新
正如Pigueiras在他的评论中指出的那样,这会破坏popover的默认模板.更好的解决方案是替换以下内容.popover-content
:
t.data('popover').tip().find('.popover-content').empty().append(r);
Run Code Online (Sandbox Code Playgroud)
2016年更新
感谢另一条评论,这是Bootstrap 3的工作代码:
t.data('bs.popover').tip().find('.popover-content').empty().append(r);
Run Code Online (Sandbox Code Playgroud)
为什么empty()
然后append()
什么时候可以更换?
t.data('popover').tip().find('.popover-content').html(r);
Run Code Online (Sandbox Code Playgroud)
编辑:
此外,第一种方法是正确的,并且当popover已经初始化并且您想要动态更改内容时工作正常(引导程序2.1).您只需'show'
再次调用popover进行刷新(内容和位置):
t.attr('data-content', r);
t.popover('show');
Run Code Online (Sandbox Code Playgroud)
同样的问题,我用这个技巧解决了,代码很冗长,只是一个例子,优化一下!
// functions container
var doc =
{
doPopover : function( item_id, title, content )
{
// get jq item
var item = $('#' + item_id );
// the trick to "refresh content" in every call
item.attr('data-content', content);
// popover
item.popover(
{
title : title,
trigger : 'manual'
}).popover('show');
},
popoverFirstCall : function()
{
this.doPopover('myDiv', 'Title', 'MyContent1');
},
popoverSecondCall : function()
{
var content = 'xxx'; // get the content in Ajax
this.doPopover('myDiv', 'Title', content);
}
}
// call funcs in your views
$(document).ready(function()
{
// first popover with first content
doc.popoverFirstCall();
// this event wich call the ajax content and refresh popover.
$('#button').on('click', $.proxy(doc, 'popoverSecondCall'));
});
Run Code Online (Sandbox Code Playgroud)
我想刷新标题的技巧也是一样的。
如果您有更好的解决方案,请告诉我!
编辑 :
我继续调查,
我们可以在插件代码中看到:
getContent: function () {
var content
, $e = this.$element
, o = this.options
content = $e.attr('data-content')
|| (typeof o.content == 'function' ? o.content.call($e[0]) : o.content)
return content
}
Run Code Online (Sandbox Code Playgroud)
因此,内容是在 attr“数据内容”上获取的,或者是在第一次调用(实例化)弹出窗口时给出的选项上获取的。
但实际上,问题是,选项被缓存并且不会在每次调用时刷新,所以必须使用:
$('item_id').attr('data-content', 'some content'); // and warning, it is different than
$('item_id').data('content', 'some content');
Run Code Online (Sandbox Code Playgroud)
并引导程序获取 attr 方式。
标题相同:
getTitle: function () {
var title
, $e = this.$element
, o = this.options
title = $e.attr('data-original-title')
|| (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
return title
}
Run Code Online (Sandbox Code Playgroud)
所以, doPopover 函数可以是:
doPopover : function( item_id, title, content )
{
// get jq item
var item = $('#' + item_id );
// the trick to "refresh content" in every call
item.attr('data-content', content); // do not use data() way.
item.attr('data-original-title', title);
// popover (trace first call if you want)
item.popover(
{
trigger : 'manual'
});
item.popover('show);
}
Run Code Online (Sandbox Code Playgroud)
对我来说工作得很好。