2

我在我的项目中使用Twitter Bootstrap了模态弹出对话框;

// when this button is click, the dialog is open    
<a type="button" class="btn" style="width:100%;" href="#test_modal" data-toggle="modal">Add Image</a>

如您所见,单击上述按钮按钮时,会打开以下对话框;

<div class="modal fade" id="test_modal">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <h3>Modal Header</h3>
  </div>
  <div class="modal-body">
    <p>Test Modal</p>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <a href="#" class="btn btn-primary">Save Changes</a>
  </div>
</div>

但是,我想为它分配一个快捷键。例如,当有人按下Ctrl+Shift+L时,我想打开上面的对话框。我不需要按下按钮。
如何在 jQuery 中实现上述功能?

4

2 回答 2

3

试试这个:点击 ctrl+m;当点击 ctrl+m 时显示模态。

$(document).on('keydown', function ( e ) {
    // You may replace `m` with whatever key you want
    if ((e.metaKey || e.ctrlKey) && ( String.fromCharCode(e.which).toLowerCase() === 'm') ) {
		$("#exampleModal").modal('show');
    }
});
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />

    <link rel="stylesheet" media="print" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">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">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

于 2018-09-04T08:06:04.933 回答
1

你可以试试这样

$(document).keydown(function(evt){
    if (evt.keyCode==108 && (evt.ctrlKey) && (evt.shiftKey)){
        evt.preventDefault();
        $('#yourModal').modal('show');
    }
});
于 2013-05-15T11:03:43.083 回答