我正在动态列表上使用TwitterBootstrap的弹出窗口。列表项有一个按钮,当我单击该按钮时,它将显示弹出窗口。当我在非动态环境下进行测试时,它可以正常工作。
这是我的非动态列表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();
});
但是,它在动态列表上效果不佳。当我单击“两次”按钮时,它可以显示,并且仅显示我单击第一时间的列表项之一。
我的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>
我的动态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');
});
我已经搜索过类似的问题,但是仍然找不到解决我的问题的方法。如果有人有什么想法,请告诉我。感谢您的帮助。
更新资料
如果您的弹出框的选择器是一致的,则可以使用selector
popover构造函数的属性。
var popOverSettings = {
placement: 'bottom',
container: 'body',
html: true,
selector: '[rel="popover"]', //Sepcify the selector here
content: function () {
return $('#popover-content').html();
}
}
$('body').popover(popOverSettings);
其他方法:
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>");
});
但是不建议将DOMNodeInsertedEvent用于性能问题和支持。也已弃此方法。因此,最好的选择是保存设置并在使用新元素更新后绑定。
另一种推荐的方法是根据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);
我看到了另一个这样的问题,但是答案对我不起作用,我不知道为什么。为什么超过50px时我的popover内容不滚动?还有一个附带问题:在我的变量a中,我的换行符在弹出窗口中不起作用。有人知道为什么吗?
本文向大家介绍如何使用jQuery在动态创建的元素上绑定事件?,包括了如何使用jQuery在动态创建的元素上绑定事件?的使用技巧和注意事项,需要的朋友参考一下 要在动态创建的元素上绑定事件,您需要动态加载。您可以尝试运行以下代码,以了解如何在动态创建的元素上绑定事件。在这里,我们将在单击按钮时生成一个新的列表项。 示例
描述 (Description) 可以使用相关的app方法动态创建popover,如下所示 - myApp.popover(popoverHTML, target, removeOnClose) - 此方法接受以下参数 popoverHTML - 它是popoverHTML的HTML字符串。 target - 它是一个HTMLElement or string (with CSS Selector
问题内容: 我使用jquery fancybox 1.3.4作为弹出表单。 但是我发现fancybox无法绑定到动态添加的元素。例如,当我向当前文档中添加html元素时。 像这样:首先我使用jquery将一个元素附加到主体, 我叫fancybox, 但fancybox不适用于动态添加的元素。 我不能从此元素调用fancybox吗? 问题答案: 将fancybox(v1.3.x)绑定到动态添加的元素
描述 (Description) 您还可以使用HTML到App方法创建动态弹出窗口。 它使用两个参数 - popupHTML - 它包含Popup内容的字符串元素。 removeOnClose - 它包含布尔值,当您关闭Popup时,它将从DOM中删除。 默认情况下,它包含真值。 例子 (Example) 以下示例演示了在Framework7中使用动态弹出窗口 - <!DOCTYPE html>
问题内容: 这个问题的答案是 社区的努力。编辑现有答案以改善此职位。它目前不接受新的答案或互动。 我有一些代码,在其中循环浏览页面上的所有选择框,并将事件绑定到它们上,以使它们的宽度变窄。 这在页面准备就绪时发生,并且工作正常。 我的问题是,在初始循环之后,我通过Ajax或DOM添加的所有选择框都没有事件绑定。 我已经找到了这个插件([jQuery Live Query Plugin](),但是在