我正在使用 Bootstrap 4,并且想要使用弹出窗口,我可以将鼠标悬停在弹出窗口中以在您单击任意位置时激活并关闭它。
我还想让链接在弹出窗口中工作。有人知道如何让它工作或者我错过了什么吗?
$(document).ready(function(){
$('[data-toggle="popover"]').popover({
placement : 'top',
trigger : 'hover',
html : true
});
});
$('body').on('click', function (e) {
//only buttons
if ($(e.target).data('toggle') !== 'popover'
&& $(e.target).parents('.popover.in').length === 0) {
$('[data-toggle="popover"]').popover('hide');
}
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<a href="mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b0a1b7b084b0a1b7b0eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>" data-toggle="popover" title="Popover" data-content="test content <a href='' title='test add link'>link on content</a>" data-original-title="test title">Test</a>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e4948b94948196ca8e97a4d5cad5d2cad4" rel="noreferrer noopener nofollow">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
最佳答案
要呈现链接,请使用 html: true
并显示/隐藏弹出窗口,您可以使用以下代码:
$(document).ready(function() {
$('[data-toggle="popover"]').popover({
placement: 'top',
html: true
});
$('[data-toggle="popover"]').on("mouseenter", function() {
$(this).popover('show');
});
$('body').on('click', function(e) {
$('[data-toggle=popover]').each(function() {
if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
$(this).popover('hide');
}
});
});
});
html, body{ height: 100%; }
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
<a href="mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="611504121521150412154f020e0c" rel="noreferrer noopener nofollow">[email protected]</a>" data-toggle="popover" title="Popover" data-content="test content <a href='#' title='test add link'>link on content</a>" data-original-title="test title">Test</a>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="27574857574255094d5467160916110917" rel="noreferrer noopener nofollow">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
关于javascript - 悬停时激活 Popover 并单击任意位置关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61197631/