我有一个用于调用modal的a
元素:
<a class="btn" data-toggle="modal" data-target="#myModal" href="http://some-url" >Launch Modal</a>
假设这是我的模式:
<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>
我可以将函数绑定到modal触发的事件:
$('#myModal').on('hidden', function () {
// do something…
})
我的问题是:如何从我的事件订阅函数访问-which调用的a
元素?
发布于 2014-01-23 18:04:35
多亏了event.relatedTarget
,它在Bootstrap 3.0.0中得到了解决。
$('#your-modal').on('show.bs.modal', function (e) {
var $invoker = $(e.relatedTarget);
});
发布于 2014-02-21 23:36:59
模态元素有一个名为modal
的数据对象,它保存传递的所有选项。您可以在触发模态的元素上设置“源”数据属性,将其设置为源的ID,然后稍后从options
变量中检索它。
触发器将是:
<a id="launch-modal-1" class="btn" data-toggle="modal" data-target="#myModal" href="http://some-url" data-source="#launch-modal-1">Launch Modal</a>
在事件回调中,获取modal
数据对象,并查看source
选项:
$('#myModal').on('hidden', function () {
var modal = $(this).data('modal');
var $trigger = $(modal.options.source);
// $trigger is the #launch-modal-1 jQuery object
});
发布于 2012-08-01 07:53:26
目前它还不能开箱即用。如果你想自己编辑bootstrap模式的代码,这里有一个特性请求和一个变通方法。
请参阅here
https://stackoverflow.com/questions/11570333
复制相似问题