如何获取 Twitter Bootstrap Modal 的调用者元素? [英] How to get Twitter Bootstrap Modal's invoker element?

查看:10
本文介绍了如何获取 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);
});

这篇关于如何获取 Twitter Bootstrap Modal 的调用者元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆