按钮调用模式对话框:单击按钮时,触发事件,结果事件引用 e.relatedTarget 未定义。那么,如何从处理程序中获取调用按钮? e 似乎不包含对调用按钮的任何引用。
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
jQuery:
$('#myModal').on('show.bs.modal', function (e) {
console.log(e.relatedTarget) // do something...
})
引用:http://getbootstrap.com/javascript/#modals
最佳答案
感谢 Geo1004 .. 我已经在使用 JS 来触发模式上的“显示”事件,但我错过了第二个参数。所以 event.relatedTarget 是未定义的。
如果其他人正在使用 JS 路由(而不是使用数据属性),我会确保您将按钮作为 jQuery 对象发送 -
$( "#myModal" ).modal( "show", $( "#buttonBeingClicked" ) );
关于twitter-bootstrap - Bootstrap 模式对话框,show.bs.modal 事件相关目标未定义。如何获得点击元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23891478/