Bootstrap 工具提示在点击时显示但在鼠标移开时隐藏
Bootstrap tooltip to show on click but hide on mouse out
我有一个将文本复制到剪贴板的按钮,我需要在用户单击该按钮时显示引导工具提示以指示文本已成功复制。
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
但是我不希望用户再次单击按钮来隐藏工具提示,因为那样会在下次不必要地复制文本。相反,我只想在用户单击或将光标移出按钮时隐藏工具提示。
可以吗?
您可以使用 Bootstrap Methods
and Events
API 如下。
function copyText() {
// do something...
}
$(function() {
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="tooltip"]').on('show.bs.tooltip', function() {
$(this).on('mouseleave', function() {
$(this).tooltip('hide');
});
});
});
body {
padding: 3rem
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
<hr>
<h4>It supports multiple elements</h4>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
我有一个将文本复制到剪贴板的按钮,我需要在用户单击该按钮时显示引导工具提示以指示文本已成功复制。
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
但是我不希望用户再次单击按钮来隐藏工具提示,因为那样会在下次不必要地复制文本。相反,我只想在用户单击或将光标移出按钮时隐藏工具提示。
可以吗?
您可以使用 Bootstrap Methods
and Events
API 如下。
function copyText() {
// do something...
}
$(function() {
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="tooltip"]').on('show.bs.tooltip', function() {
$(this).on('mouseleave', function() {
$(this).tooltip('hide');
});
});
});
body {
padding: 3rem
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
<hr>
<h4>It supports multiple elements</h4>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>
<a onclick="copyText()" role="button" data-toggle="tooltip" data-theme="dark" data-trigger="click" title="Link Copied">
<i class="fas fa-share-alt"></i>
</a>