twitter-bootstrap - Bootstrap 模式对话框,show.bs.modal 事件相关目标未定义。如何获得点击元素?

标签 twitter-bootstrap events dialog modal-dialog fuelux

按钮调用模式对话框:单击按钮时,触发事件,结果事件引用 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">&times;</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/

相关文章:

vba - Excel VBA : How To Disable Protected Sheet Dialog On Keydown

javascript - 下拉子菜单不会保持打开状态

html - bootstrap cols 中字体大小的不同显示

css - Twitter Bootstrap 在 CSS 中使用带星号的宽度属性

html - Bootstrap 3 Changing font-size Affecting radio and checkbox 内衬文本

javascript - 为什么到处都支持 onreadystatechange 事件,但 document.readyState 却不支持?

c++ - 系统事件 EVENT_SYSTEM_SWITCHSTART 和 EVENT_SYSTEM_SWITCHEND 在 Windows 7 中有效,但在 Windows 10 中无效

Javascript 从数组中检索购物车的值?

Android对讲不读取对话框内容

java - 无法弄清楚如何显示 Android 对话框