ova*_*lek 5 javascript jquery twitter-bootstrap bootstrap-popover
我有一组动态的 contenteditable div。具有“.showPopover”类的 Div 将有一个弹出窗口。弹出框触发器设置为手动,因为我希望它们出现在焦点上,但并不总是隐藏在模糊处。
我在这里找到[问题]:带有手动触发器和选择器选项的 Bootstrap Tooltip ,我无法将“选择器方法”与手动触发器一起使用,所以我按照那里的答案之一进行操作,但弹出窗口仍然没有出现动态添加的div。
问题是,我只想为具有特定类的 div 显示弹出窗口,该类不与 div 一起添加。
弹出窗口的 div 类的更改通过启用按钮进行了一些简化。
jQuery(document).ready(function($) {
$('a.add').on('click', function(event) {
event.preventDefault();
$('.container').append('<p class="input" contenteditable="true"></p>');
});
$('a.enable').on('click', function(event) {
event.preventDefault();
$('.input').not('.showPopover').addClass('showPopover');
});
$('.container').on('focus', '.input.showPopover', function(event) {
if (!$(this).data("bs.popover")) {
$(this).popover({
placement:'right',
trigger:'manual',
html:true,
content:'<a href="#" class="btn btn-danger">Remove</a>'
});
}
$(this).popover('show');
});
var mousedownHappened = false;
$('.container').on('blur', '.input', function(event) {
if(mousedownHappened) {
mousedownHappened = false;
} else {
$(this).popover('hide');
}
});
$('.container').on('mousedown', '.popover .btn', function(event) {
mousedownHappened = true;
});
});
Run Code Online (Sandbox Code Playgroud)
Jsfiddle: http: //jsfiddle.net/Lh2rpj0f/2/
Jquery 1.11.1,引导程序 3.3.2
所以感谢 Yenne Info,我得到了一个可行的解决方案: http://jsfiddle.net/Lh2rpj0f/4/
它可能不是最好的解决方案,但它正是我想要的。(当我单击弹出窗口内的按钮时,单击“启用”按钮时不会破坏该弹出窗口。)
至于现在,我的最终解决方案:Bootstrap popover,在动态内容上附加手动触发器
我更新了原来的代码,现在它也按我的预期工作了。
$('.container').on('focus', '.input.showPopover', function(event) {
if (!$(this).data("bs.popover") || !$(this).attr('data-popoverAttached')) {
$(this).popover('destroy').popover({
placement:'right',
trigger:'manual',
html:true,
content:'<a href="#" class="btn btn-danger">Remove</a>'
});
$(this).attr('data-popoverAttached', true);
}
$(this).popover('show');
});
Run Code Online (Sandbox Code Playgroud)
JSfiddle:http://jsfiddle.net/Lh2rpj0f/5/
但我仍然认为引导程序弹出窗口代码中有问题。我认为我的原始代码不起作用的原因是引导弹出窗口以某种方式神奇地附加(使用默认选项!)到我所有动态添加的 div(即使它们没有类 .showPopover)。因此,当焦点触发时,它不会通过 if 语句。data-popoverAttached 属性解决了这个问题。