我正在使用 Bootstrap 弹出窗口并有一个 <select>弹出框内的字段,以便用户更改语言。
如果他们在弹出窗口外单击,我希望它消失,所以我使用了 data-trigger="focus" <a> 中的属性标记来完成此操作。
但是,如果他们点击 <select>下拉菜单,弹出窗口会在他们单击语言之前消失。
以下是供您引用的 Bootstrap - 非常感谢您的帮助。
http://www.bootply.com/SEM4ophIhx
Javascript:
$(function () {
$('[data-toggle="popover"]').popover()
})
$(function () {
$('[rel="popover"]').popover({
container: 'body',
html: true,
content: function () {
var clone = $($(this).data('popover-content')).clone(true).removeClass('hide');
return clone;
}
}).click(function (e) {
e.preventDefault();
});
});
HTML:
<a href="#" role="button" data-placement="right" data-trigger="focus" rel="popover" data-popover-content="#profilesettingsaction">
<span class="glyphicon glyphicon-cog"></span>
</a>
<div id="profilesettingsaction" class="hide">
<ul>
<li>
<select name="language">
<option value="">العربية: الإمارات العربية المتحدة</option>
<option value="">中国</option>
<option value="">中國</option>
<option value="">Nederlands: Nederland</option>
<option value="">English: United Kingdom</option>
<option value="" selected="">Language: English</option>
<option value="">Français: France</option>
<option value="">Italiano: l'Italia</option>
<option value="">日本語:日本</option>
<option value="">Português: Portugal</option>
<option value="">Español: México</option>
</select>
</li>
</ul>
</div>
最佳答案
$('[data-toggle="popover"]').popover();
$('body').on('click', function (e) {
$('[data-toggle="popover"]').each(function () {
//the 'is' for buttons that trigger popups
//the 'has' for icons within a button that triggers a popup
if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
$(this).popover('hide');
}
});
});
您只需捕获正文上的点击事件并检查目标是否是您的弹出窗口的子项。然而,这真的很慢。
关于javascript - Bootstrap3 popover data-trigger=focus 在弹出窗口中单击 <select> 输入时关闭弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36507782/