【问题标题】:How to add a tooltip to a modal launching button如何将工具提示添加到模式启动按钮
【发布时间】:2020-08-25 21:03:56
【问题描述】:

我正在使用BootstrapjQuery 向应用程序添加一些组件。我还想为其中一些组件添加工具提示。我正在使用jQuery 在我的index.html 页面中显示工具提示:

<script>
    $(function () {
        $('[data-toggle="tooltip"]').tooltip()
    })
</script>

要使用它,我只需将data-toggle="tooltip" title="Tooltip goes here" 添加到我想要应用工具提示的元素标记中。 我还有一个使用data-toggle="modal", which comes from Bootstrap` 启动模式的按钮。我不能简单地使用以下内容来添加工具提示和模式启动功能:

 <button
   type="button"
   class="btn"
   data-toggle="tooltip"
   data-target="#exampleModalCenter"
   data-toogle="modal"
   title="This launches the Program Form window"
 >
 Sign Up!
 </button>

在此按钮上获得工具提示和模态功能的简单方法是什么?

【问题讨论】:

    标签: javascript jquery bootstrap-modal tooltip


    【解决方案1】:

    使用 Jquery 点击功能调用引导模式将解决您的问题。

    片段:

    $(".btn").click(function() {
      $('#exampleModal').modal('show');
    });
    
    $('[data-toggle="tooltip"]').tooltip()
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" 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/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    
    
    <div id="exampleModal" class="modal" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>Modal body text goes here.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
    
    <div class="container">
      <div class="row" style="margin-top:50px">
        <div class="col text-center align-self-center">
          <button type="button" class="btn btn-primary" data-toggle="tooltip" title="This launches the Program Form window">Sign Up!</button>
        </div>
      </div>
    </div>

    【讨论】:

    • 这很好用。我不得不调整元素标签,但这很容易。
    【解决方案2】:

    您可以尝试通过使用 jquery 为模态功能添加点击事件,然后添加工具提示方法。

    例如:

    $(function () {
      $('[data-toggle="tooltip"]').click(function(){
        $("#myModal").modal();
      });
      $('[data-toggle="tooltip"]').tooltip();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2021-03-20
      • 1970-01-01
      • 2015-07-16
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多