use*_*267 48 javascript jquery dom popover twitter-bootstrap
我在动态列表中使用Twitter Bootstrap的popover.列表项有一个按钮,当我单击按钮时,它应该显示弹出窗口.当我在非动态测试时,它工作正常.
这是我的非动态列表的JavaScript
$("button[rel=popover]").popover({
placement : 'right',
container : 'body',
html : true,
//content:" <div style='color:red'>This is your div content</div>"
content: function() {
return $('#popover-content').html();
}
})
.click(function(e) {
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
但是,它在动态列表上不能很好地工作.当我单击"两次"按钮时它会显示出来,并且只显示一个列表项我点击一次.
我的HTML:
<ul id="project-list" class="nav nav-list">
<li class='project-name'>
<a >project name 1
<button class="pop-function" rel="popover" ></button>
</a>
</li>
<li class='project-name'>
<a>project name 2
<button class="pop-function" rel="popover" ></button>
</a>
</li>
</ul>
<div id="popover-content" style="display:none">
<button class="pop-sync"></button>
<button class="pop-delete"></button>
</div>
Run Code Online (Sandbox Code Playgroud)
我的动态JavaScript:
$(document).on("click", "#project-list li" , function(){
var username = $.cookie("username");
var projectName = $(this).text()
$("li.active").removeClass("active");
$(this).addClass("active");
console.log("username: " +username + " project name: "+projectName );
});
$(document).on("click", "button[rel=popover]", function(){
$(this).popover({
placement : 'right',
container : 'body',
html : true,
content: function() {
return $('#popover-content').html();
}
}).click(function(e){
e.preventDefault();
})
});
//for close other popover when one popover button click
$(document).on("click", "button[rel=popover]" , function(){
$("button[rel=popover]").not(this).popover('hide');
});
Run Code Online (Sandbox Code Playgroud)
我搜索过类似的问题,但我仍然无法找到解决问题的方法.如果有人有想法,请告诉我.谢谢你的帮助.
PSL*_*PSL 86
更新
如果您的popover将具有一致的选择器,那么您可以使用selectorpopover构造函数的属性.
var popOverSettings = {
placement: 'bottom',
container: 'body',
html: true,
selector: '[rel="popover"]', //Sepcify the selector here
content: function () {
return $('#popover-content').html();
}
}
$('body').popover(popOverSettings);
Run Code Online (Sandbox Code Playgroud)
其他方法:
Mutation Event/ Mutation Observer来标识是否已将特定元素插入到ul元素或元素中.var popOverSettings = { //Save the setting for later use as well
placement: 'bottom',
container: 'body',
html: true,
//content:" <div style='color:red'>This is your div content</div>"
content: function () {
return $('#popover-content').html();
}
}
$('ul').on('DOMNodeInserted', function () { //listed for new items inserted onto ul
$(event.target).popover(popOverSettings);
});
$("button[rel=popover]").popover(popOverSettings);
$('.pop-Add').click(function () {
$('ul').append("<li class='project-name'> <a>project name 2 <button class='pop-function' rel='popover'></button> </a> </li>");
});
Run Code Online (Sandbox Code Playgroud)
但是不建议使用DOMNodeInserted Mutation Event来解决性能问题以及支持.这也已被弃用.因此,您最好的选择是在使用新元素更新后保存设置并绑定.
另一种推荐的方法是根据MDN 使用MutationObserver而不是MutationEvent,但是在某些浏览器中再次支持是未知的并且性能是一个问题.
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
// create an observer instance
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
$(mutation.addedNodes).popover(popOverSettings);
});
});
// configuration of the observer:
var config = {
attributes: true,
childList: true,
characterData: true
};
// pass in the target node, as well as the observer options
observer.observe($('ul')[0], config);
Run Code Online (Sandbox Code Playgroud)
小智 12
可能太晚了,但这是另一种选择:
$('body').popover({
selector: '[rel=popover]',
trigger: 'hover',
html: true,
content: function () {
return $(this).parents('.row').first().find('.metaContainer').html();
}
});
Run Code Online (Sandbox Code Playgroud)
我这样做了,它对我有用."content"是placesContent对象.不是HTML内容!
var placesContent = $('#placescontent');
$('#places').popover({
trigger: "click",
placement: "bottom",
container: 'body',
html : true,
content : placesContent,
});
$('#places').on('shown.bs.popover', function(){
$('#addPlaceBtn').on('click', addPlace);
}
Run Code Online (Sandbox Code Playgroud)
<div id="placescontent"><div id="addPlaceBtn">Add</div></div>
Run Code Online (Sandbox Code Playgroud)