英文:
Conditional Bootstrap tooltip
问题
我有这段非常简单的Javascript代码:
$(""#myinput").hover(function(){
if(condition){
$(this).tooltip({placement: "bottom", title: "mytitle"});
}
);
以及它的HTML:
<input data-toggle="tooltip" type="password" placeholder="Enter your Password" id="myinput" name="password" required />
当我加载页面时,条件是false,所以工具提示不会显示,当条件变为true时,它开始显示。问题是,当条件再次变为false时,即使不应该显示,工具提示仍然会在悬停时显示。我该如何修复这个问题?
英文:
I have this very simple Javascript code:
$("#myinput").hover(function(){
if(condition){
$(this).tooltip({placement: "bottom", title: "mytitle"});
}
);
with its HTML:
<input data-toggle="tooltip" type="password" placeholder="Enter your Password" id="myinput" name="password" required />
When I load the page the condition it's false so the tooltip correctly doesn't show and when it becomes true it starts showing. The problem is that when the condition goes back to false the tooltip keeps showing when hovering even if it shouldn't. How can I fix this?
答案1
得分: 2
你需要为假条件添加一个 else 语句,并调用 $(this).tooltip("dispose");
来关闭工具提示。否则,工具提示将始终保持启用。
Bootstrap 5 文档关于 #dispose 以及所有工具提示的 方法。
工具提示切换的小演示。
let condition = true;
$("#myinput").hover(function () {
if (condition) {
$(this).tooltip({ placement: "bottom", title: "mytitle" });
} else {
$(this).tooltip("dispose");
}
});
const button = document.querySelector("#button");
button.addEventListener("click", function () {
condition = !condition;
if (condition) {
button.textContent = "Turn Tooltip Off";
} else {
button.textContent = "Turn Tooltip On";
}
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input data-toggle="tooltip" type="password"
placeholder="Enter your Password"
id="myinput" name="password"
required />
<button id="button">Turn Tooltip Off</button>
英文:
You will need to add an else statement for your false condition and call $(this).tooltip("dispose");
to get rid of the tooltip. Otherwise tooltip will always be enabled.
Bootstrap 5 documentation on #dispose and all its tooltip methods.
Small demo of toggling tooltip.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let condition = true;
$("#myinput").hover(function () {
if (condition) {
$(this).tooltip({ placement: "bottom", title: "mytitle" });
} else {
$(this).tooltip("dispose");
}
});
const button = document.querySelector("#button");
button.addEventListener("click", function () {
condition = !condition;
if (condition) {
button.textContent = "Turn Tooltip Off";
} else {
button.textContent = "Turn Tooltip On";
}
});
<!-- language: lang-html -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input data-toggle="tooltip" type="password"
placeholder="Enter your Password"
id="myinput" name="password"
required />
<button id="button">Turn Tooltip Off</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论