如何获取 Twitter Bootstrap Modal 的调用者元素?

我有一个用于调用模态的 a 元素:

I have got a a element for invoking modal:

<a class="btn" data-toggle="modal" data-target="#myModal" href="http://some-url" >Launch Modal</a>

而且,说这是我的模态:

And, say this is my modal:

<div class="modal hide" id="myModal">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3>Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</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>

我可以将函数绑定到模态触发的事件:

I can bind functions to events fired by modal:

$('#myModal').on('hidden', function () {
  // do something…
})

我的问题是:如何从我的事件订阅函数访问 a 元素 - 它调用了模态 -?

My question is: How can i access the a element -which invoked the modal- from my event subscriber function?

推荐答案

由于 event.relatedTarget 在 Bootstrap 3.0.0 中解决了.

It was solved in Bootstrap 3.0.0 thanks to event.relatedTarget.

$('#your-modal').on('show.bs.modal', function (e) {
  var $invoker = $(e.relatedTarget);
});

相关文章