微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

twitter-bootstrap – 引导模态对话框,show.bs.modal事件relatedTarget未定义.如何获得点击元素?

按钮调用模态对话框:当单击按钮时,事件触发生成的事件引用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

解决方法

看看下面的 Bootply example.

当运行show事件似乎包括对e.relatedTarget的适当引用.

$('#myModal').on('show.bs.modal',function (e) {
    var button = e.relatedTarget;
    if (button != null)
    {
        alert("Launch Button ID='" + button.id + "'");
    }
})

看看Bootply example,看看你自己的代码是否偏离了它. (我从您提供的link直接复制了原始的Bootstrap示例代码片段.)

我希望这有帮助.祝你好运.

原文地址:https://www.jb51.cc/bootstrap/234097.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐