条件式引导提示

huangapple go评论76阅读模式
英文:

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(&quot;dispose&quot;); 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;

$(&quot;#myinput&quot;).hover(function () {
    if (condition) {
        $(this).tooltip({ placement: &quot;bottom&quot;, title: &quot;mytitle&quot; });
    } else {
        $(this).tooltip(&quot;dispose&quot;);
    }
});

const button = document.querySelector(&quot;#button&quot;);
button.addEventListener(&quot;click&quot;, function () {
    condition = !condition;
    if (condition) {
        button.textContent = &quot;Turn Tooltip Off&quot;;
    } else {
        button.textContent = &quot;Turn Tooltip On&quot;;
    }
});

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input data-toggle=&quot;tooltip&quot; type=&quot;password&quot; 
       placeholder=&quot;Enter your Password&quot; 
       id=&quot;myinput&quot; name=&quot;password&quot; 
       required /&gt;


&lt;button id=&quot;button&quot;&gt;Turn Tooltip Off&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月20日 23:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定