gpt4 book ai didi

javascript - 为什么 Bootstrap 工具提示没有初始化两次?

转载 作者:太空宇宙 更新时间:2023-11-04 09:08:51 25 4
gpt4 key购买 nike

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.js"></script>
</head>
<body>

<div class="container">
<h3>Tooltip Example</h3>
<a data-toggle="tooltip" title="Hooray!">Hover over me</a>
</div>

<script>
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="tooltip"]').off("mouseenter mouseleave");
$('[data-toggle="tooltip"]').tooltip();

});
</script>

</body>
</html>

为什么即使我初始化了两次也无法获得工具提示??

最佳答案

DavidG 的评论与记录。基本上,如果您希望能够再次调用 tooltip 并让它附加监听器,则需要完全销毁 tooltip。通过调用 off,您只是删除了监听器,而不是破坏了 tooltip。因此,当您第二次调用 tooltip 时,它不会再次初始化 tooltip。因为下面一行:

if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))

https://github.com/twbs/bootstrap/blob/v3-dev/js/tooltip.js#L501

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.js"></script>
</head>
<body>

<div class="container">
<h3>Tooltip Example</h3>
<a data-toggle="tooltip" data-placement="top" title="Hooray!">Hover over me</a>
</div>

<script>
$(document).ready(function(){
console.log("init");
$('[data-toggle="tooltip"]').tooltip();
setTimeout(function() {
console.log("destroy");
$('[data-toggle="tooltip"]').tooltip("destroy");
setTimeout(function() {
console.log("reinit");
$('[data-toggle="tooltip"]').tooltip();
}, 5000);
}, 5000);
});
</script>

</body>
</html>

关于javascript - 为什么 Bootstrap 工具提示没有初始化两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42275025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com