【问题标题】:Bootstrap 4 Tooltip Persists, Looks TerribleBootstrap 4 工具提示仍然存在,看起来很糟糕
【发布时间】:2019-06-26 05:25:36
【问题描述】:

您能否运行以下 html 测试用例,以帮助确定当工具提示位于按钮上方时按钮被禁用时如何使 bootstrap 4 工具提示消失?代码中列出了有关我的问题的详细信息。

stackoverflow 编辑器不满意我将细节作为 cmets 放入代码中,因此我将在此重复一些细节以尝试让编辑器满意。当光标悬停在按钮上时禁用按钮时,工具提示不会消失,而且看起来很糟糕。任何帮助表示赞赏。

谢谢

 <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Bootstrap 4 Tooltip Persists, Looks Terrible</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="container">
    <h3>Bootstrap 4 Tooltip Persists, Looks Terrible</h3>
    <br> "Other button disabled" scenario works fine.
    <br> First, hover over buttons. Look at tooltips while hovering.
    <br> Then, click button A to disable button B. Works fine:
    <br> - Button B is disabled, and no tooltip.
    <br> - Tooltip still works correctly on button A.
    <br> <br>
    <button type="button" id="button_a" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button B" onclick='change_button ("button_b", true);'>
    Button A
    </button>
    <button type="button" id="button_b" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disabled by Button A">
    Button B
    </button>
    <br> <br> <hr>
    <br> "Same button disabled" scenario fails.
    <br> First, hover over Button C. Look at the tooltip.
    <br> Then, click button C to disable button C. Messes up:
    <br> - Button C is disabled, so that part is OK.
    <br> - But Button C tooltip persists, looks terrible.
    <br> <br>
    <button type="button" id="button_c" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button C" onclick='change_button ("button_c", true);'>
    Button C
    </button>
    <button type="button" id="button_d" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Enables Button C" onclick='change_button ("button_c", false);'>
    Button D
    </button>
    <br> <br> <hr> <br>
    So if a button is disabled while the cursor is hovering over it, the tooltip persists.
    <strong>
    Could you please explain how to make tooltip go away when Button C is disabled?
    </strong>
    I would prefer if tooltip went away when Button C disabled,
    since that is default behavior of bootstrap disabled buttons.
    And disabled button may later get enabled again, so I do not want to delete title attribute.
    Basically I want bootstrap to behave the same way as when Button B is disabled.
    I have looked around and cannot find how to fix this problem,
    and have tried various things, with no success.
    There are real instances where a button carries out an action,
    and then the button needs to be disabled, so I appreciate the help. Thanks
    </div>
    <script>
    $(document).ready(function(){$('[data-toggle="tooltip"]').tooltip();});
    function change_button (button_id, bool_val) {
    var button_obj = document.getElementById (button_id);
    button_obj.disabled = bool_val;
    }
    </script>
    </body>
    </html>

【问题讨论】:

    标签: jquery css twitter-bootstrap bootstrap-4


    【解决方案1】:

    只需将工具提示的hidedisableenable 的逻辑添加到您的change_button() 函数中。

    $(document).ready(function() {
      $('[data-toggle="tooltip"]').tooltip();
    });
    
    function change_button(button_id, bool_val) {
      var button_obj = document.getElementById(button_id);
      button_obj.disabled = bool_val;
    
      // Disable tooltip
      if (bool_val) {
        $('#' + button_id).tooltip('hide').tooltip('disable');
      } else {
        $('#' + button_id).tooltip('enable');
      }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
    
    <div class="container">
      <br> "Other button disabled" scenario works fine.
      <br> First, hover over buttons. Look at tooltips while hovering.
      <br> Then, click button A to disable button B. Works fine:
      <br> - Button B is disabled, and no tooltip.
      <br> - Tooltip still works correctly on button A.
      <br> <br>
      <button type="button" id="button_a" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button B" onclick='change_button ("button_b", true);'>
        Button A
        </button>
      <button type="button" id="button_b" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disabled by Button A">
        Button B
        </button>
      <br> <br>
      <hr>
      <br> "Same button disabled" scenario fails.
      <br> First, hover over Button C. Look at the tooltip.
      <br> Then, click button C to disable button C. Messes up:
      <br> - Button C is disabled, so that part is OK.
      <br> - But Button C tooltip persists, looks terrible.
      <br> <br>
      <button type="button" id="button_c" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button C" onclick='change_button ("button_c", true);'>
        Button C
        </button>
      <button type="button" id="button_d" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Enables Button C" onclick='change_button ("button_c", false);'>
        Button D
        </button>
    </div>

    【讨论】:

    • 谢谢。您的添加非常适合测试用例。我不知道 jquery,但可能可以使它在生产版本中工作。但是,使用引导程序或 CSS 参数是否有更简单的替代方案?还是需要 jquery 才能完成这项工作?再次感谢
    • 正如您在documentation 中看到的,Bootstrap 方法通过 jQuery 运行。所以你需要 jQuery并且你正在使用 Bootstrap。
    • 是的,我理解您的观点,但感谢您强调它们。通过说“引导程序或 CSS 参数”,我的意思是可能添加的 CSS 类,或者对现有引导程序 CSS 类进行一些修改。显然,如果可以以某种方式一次性配置引导程序,而不是编写 jquery 代码,那会更简单。无论如何,感谢您验证我需要 jquery 来解决问题,并为我指明方向。
    【解决方案2】:

    您必须在按钮禁用后销毁工具提示。

      function change_button(button_id, bool_val) {
        var button_obj = document.getElementById(button_id);
        $("#" + button_id).tooltip("hide");
        button_obj.disabled = bool_val;
      }
    

    【讨论】:

    • 谢谢。您的添加也非常适合测试用例,并且只是一条简单的线。同样,我不知道 jquery(我非常擅长 javascript、css、html,避免使用 jquery),所以我很欣赏你对(对我来说)晦涩语法的专业知识。再次,使用引导程序或 CSS 参数的任何更简单的替代方案?知道当前哪种解决方案“更好”吗?再次感谢
    • 因为tooltip不是jquery函数,是bootstrap提供的。所以你可以通过 bootstrap js 本身来控制它的属性。在这里您可以查看 BS 工具提示 w3schools.com/bootstrap4/bootstrap_ref_js_tooltip.asp 的所有可用选项
    • 我很难找出哪个答案是“最有帮助的”。我选择你的答案是因为 1)更短/更简单,2)你先回答,3)我需要选择一个人。两个答案都非常有帮助。如果其他人有答案或 cmets,我很想听听。
    • @user3133452 请记住,答案并不相同。这个答案只是第一次隐藏工具提示,但是如果您在禁用时再次悬停按钮,它将显示工具提示。这就是为什么我做了这两件事(隐藏和禁用)然后重新启用它。
    • @user3133452,非常感谢!
    【解决方案3】:

    我是原创海报。我对 azeos 表示这两个答案之间的行为不同感到惊讶,因为我在测试浏览器 IE 11 和 chrome 74 中没有发现任何差异。所以到目前为止,我对这两个答案进行了更多测试。两个答案都修改了原始帖子中的 change_button 功能:

    function change_button(button_id, bool_val) { /* A */
      var button_obj = document.getElementById(button_id);
      $("#" + button_id).tooltip("hide");
      button_obj.disabled = bool_val;
      }
    

    //////

    function change_button(button_id, bool_val) { /* B */
      var button_obj = document.getElementById(button_id);
      button_obj.disabled = bool_val;
      if (bool_val) {
        $('#' + button_id).tooltip('hide').tooltip('disable');
        }
      else {
        $('#' + button_id).tooltip('enable');
        }
      }
    


    澄清一下,正确的(与其他引导工具提示一致)行为是:

    • 加载页面
    • 按钮 C 在悬停时有工具提示
    • 单击按钮 C
    • 按钮 C 被禁用 并且 按钮 C 工具提示消失
    • 单击按钮 D
    • 按钮 C 已启用,并且在悬停时再次显示工具提示


    以下是我进一步测试的结果:

    • IE 11(Windows):答案 A 和 B 都有效
    • chrome 74(Windows):答案 A 和 B 都有效
    • opera 60 (windows):答案 A 和 B 都有效
    • safari 10 (mac):答案 A 和 B 都有效
    • firefox 67(Windows):答案 A 失败,B 有效。


    使用 firefox 67,Button C 工具提示并没有消失,因为 azeos 建议可能是一个问题。

    当我有更多的时间时,我会做更多的测试。另外,我在 github 上的 twbs/bootstrap 上发帖报告这是一个错误,或者至少是一个问题,并将关注任何回复。

    我使用 lambdatest 网站进行 safari 测试。我直接在 Windows 7 桌面上进行了其他测试。

    我们将不胜感激任何其他 cmets 或建议,例如任何使答案 B 失败的测试,或任何智能手机测试。

    【讨论】:

    • 我做了更多测试:iphone 上的 safari:答案 A 和 B 都有效。 android 手机上的 chrome (galaxy note ii):答案 A 失败,B 有效。同样,失败是按钮 C 工具提示仍然存在,不会消失。所以答案 B 工作正常,AFAICT。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多