javascript - 嵌入式视频无法以模式打开

标签 javascript jquery bootstrap-4 youtube bootstrap-modal

我正在尝试以模式打开我的视频。我正在使用 javascript 创建按钮,在点击方法上我想打开带有嵌入式 youtube 视频的模式,但我一直收到 404 错误。我的错误是什么:

我的 json 中有视频 ID,我从那里调用视频 ID。包含表格的模态应使用 youtube 视频调用另一个模态

我的代码:

HTML:

<div class="modal" tabindex="-1" role="dialog" id="drilldown">
                            <div class="modal-dialog modal-lg" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title pull-left"></h5>
                                        <div class="pull-right">

                                            <a class="close" data-dismiss="modal" aria-label="Close" style="cursor: pointer;">
                                                <span aria-hidden="true"><i class="fas fa-times"></i></span>
                                            </a>
                                        </div>
                                    </div>
                                    <div class="modal-body">
                                        <div class="spin-wrapper">
                                            <div class="spinner"></div>
                                        </div>
                                        <table id="table"  class="table table-sm table-hover">
                                            <thead>
                                                <tr>
                                                    <th>Date</th>
                                                    <th>Name</th>
                                                    <th>Youtube</th>
                                                </tr>
                                            </thead>
                                            <tbody></tbody>
                                        </table>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">


                                    <div class="modal-body">

                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>        
                                        <!-- 16:9 aspect ratio -->
                                        <div class="embed-responsive embed-responsive-16by9">
                                            <iframe class="embed-responsive-item" src="" id="video"  allowscriptaccess="always" allow="autoplay"></iframe>
                                        </div>


                                    </div>

                                </div>
                            </div>
                        </div>

JavaScript

json.forEach(elem => {
elem.youtube = `<a class="vl" id="${elem.video_id}" data-toggle="modal" data-src="https://www.youtube.com/embed/${elem.video_id}?autoplay=1" data-target="#myModal"><i class="fab fa-youtube"></i></a>
 </a>`;
});

 let table = $('#table').DataTable({
        data: json,
        columns: [{
          data: 'date'
        }, {
          data: 'name'
        }, {
          data: 'youtube'
        }]
      });
});

let $videoSrc;
$('.vl').click(function() {
  console.log(this);
  $videoSrc = $(this).data("src");
  // console.log($videoSrc);
});

$('#myModal').on('shown.bs.modal', function(e) {

  $("#video").attr('src', $videoSrc + "?autoplay=1&amp;modestbranding=1&amp;showinfo=0");
})

$('#myModal').on('hide.bs.modal', function(e) {
  $("#video").attr('src', $videoSrc);
})

当我点击模式时,出现 404 错误。

最佳答案

以下将起作用,但您不能在此处执行此操作,因为 YouTube 视频会触发跨站点警告。

A cookie associated with a cross-site resource at https://youtube.com/ was set without the SameSite attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with SameSite=None and Secure

我在这里创建了一个工作演示:https://jsfiddle.net/MrPolywhirl/5xyetqw2/

let json = [{
  date: '2019-04-30',
  name: 'Stack Overflow for Teams - Q&A in a Private and Secure Environment',
  video_id: 'HJtJXMKpl2g'
}, {
  date: '2019-06-04',
  name: 'Stack Overflow Talent - The Best Way to Hire Developers',
  video_id: 'PMDmo4SaP1s'
}, {
  date: '2019-07-19',
  name: 'Stack Overflow for Teams - Share Knowledge & Drive Productivity',
  video_id: 'wmzBz5WSEls'
}];

var $videoSrc = null;

json.forEach(elem => {
  elem.youtube = $('<a>', { id: elem.video_id, class: 'video_link' })
    .attr('data-toggle', 'modal')
    .attr('data-src', `https://www.youtube.com/embed/${elem.video_id}`)
    .attr('data-target', '#video-modal')
    .append($('<i>').addClass('fab fa-youtube'))[0].outerHTML;
});

let table = $('#video-table').DataTable({
  data: json,
  columns: [{
    title: 'Date',
    data: 'date'
  }, {
    title: 'Title',
    data: 'name'
  }, {
    title: 'Link',
    data: 'youtube'
  }]
});

$('#toggle-modal-btn').on('click', function(e) {
  $('.modal').first().modal('toggle');
});

$('#toggle-modal-btn').trigger('click'); // Show the table modal

$('.video_link').click(function(e) {
  $videoSrc = $(this).data('src'); // Update the video source
});

$('#video-modal').on('shown.bs.modal', function(e) {
  $("#video-frame").attr('src', $videoSrc + "?autoplay=1&amp;modestbranding=1&amp;showinfo=0");
});

$('#video-modal').on('hide.bs.modal', function(e) {
  $("#video-frame").attr('src', $videoSrc);
});
.video_link i {
  color: red;
  cursor: pointer;
}

#toggle-modal-btn {
  display: block;
  width: 8em;
  height: 2.5em;
  margin: 0 auto;
  margin-top: calc(50vh - 2.5em);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js"></script>
<button id="toggle-modal-btn">Show Modal</button>
<div class="modal" tabindex="-1" role="dialog" id="drilldown">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title pull-left"></h5>
        <div class="pull-right">
          <a class="close" data-dismiss="modal" aria-label="Close" style="cursor: pointer;">
            <span aria-hidden="true"><i class="fas fa-times"></i></span>
          </a>
        </div>
      </div>
      <div class="modal-body">
        <div class="spin-wrapper">
          <div class="spinner"></div>
        </div>
        <table id="video-table" class="table table-sm table-hover"></table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" id="video-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <!-- 16:9 aspect ratio -->
        <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item" src="" id="video-frame" allowscriptaccess="always" allow="autoplay"></iframe>
        </div>
      </div>
    </div>
  </div>
</div>

关于javascript - 嵌入式视频无法以模式打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58490508/

相关文章:

html - 将引导卡中的图像居中

javascript - learnyounode 文件服务器 为什么要把 createReadStream() 放在 createServer() 里面?

javascript - 当我运行 server.js 和 client.js 并访问 localhost 时,为什么会收到错误请求?

javascript - Promise 被拒绝并带有非错误警告

jquery - masonry 元素 margin 底部问题

css - 如何在 Booststrap 和 wagtail 中使用 css 类

javascript - 在indexedDB中添加对象时报错 "Data provided to an operation does not meet requirements"

javascript - 如何在mapbox GL JS上按日期范围过滤数据?

c# - ASP.net C# 中的动态警告框

modal-dialog - 如何垂直对齐Bootstrap v4模式对话框