如何将工具提示添加到模式启动按钮

时间:2020-08-25 21:03:56

标签: javascript jquery bootstrap-modal tooltip

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

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

要使用此功能,我只需将data-toggle="tooltip" title="Tooltip goes here"添加到要在其上应用工具提示的element标签。 我还有一个按钮,可以使用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>

在此按钮上获取工具提示和模式功能的简便方法是什么?

2 个答案:

答案 0 :(得分:1)

您可以尝试通过添加带有jquery的click事件来获取模态函数,然后添加工具提示方法。

例如:

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

答案 1 :(得分:1)

使用Jquery click函数调用引导程序模式将解决您的问题。

代码段:

$(".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>

相关问题