【发布时间】:2019-03-28 16:12:14
【问题描述】:
Firefox 在控制台中显示以下错误:
ReferenceError: 事件未定义
参考我的代码,它允许我以全屏模式打开嵌入的 youtube 视频。
$(document).ready(function () {
$(".vma_overlay").click(function () {
var $videoSrcOriginal = $(event.target).siblings('.vma_iFramePopup').attr("src");
// Check if the embedded youtube url has any attributes appended
// by looking for a '?' in the url.
// If one is found, append our autoplay attribute using '&',
// else append it with '?'.
if ($videoSrcOriginal.indexOf('?') > -1) {
var $videoSrc = $videoSrcOriginal
// when the modal is opened autoplay it
$('#vma_ModalBox').on('shown.bs.modal', function (e) {
// set the video src to autoplay
var $videoSrcAuto = $videoSrc + "&autoplay=1&mute=1";
$("#vma_video").attr('src', $videoSrcAuto);
$('body').addClass("modalyt");
})
} else {
var $videoSrc = $(".vma_iFramePopup").attr("src");
// when the modal is opened autoplay it
$('#vma_ModalBox').on('shown.bs.modal', function (e) {
// set the video src to autoplay
var $videoSrcAuto = $videoSrc + "?autoplay=1&mute=1";
$("#vma_video").attr('src', $videoSrcAuto);
$('body').addClass("modalyt");
})
}
// stop playing the youtube video when modal is closed
$('#vma_ModalBox').on('hide.bs.modal', function (e) {
$("#vma_video").attr('src', $videoSrc);
$('body').removeClass("modalyt");
})
});
});
Firefox 突出显示以下代码行是罪魁祸首:
var $videoSrcOriginal = $(event.target).siblings('.vma_iFramePopup').attr("src");
我在 Chrome、IE 或 Edge 中似乎没有遇到此问题。
我已尝试将所有内容放在 CodePen 中:https://codepen.io/CodeChaos/pen/ZPgbJe
【问题讨论】:
-
为什么要定义
event? -
$(".vma_overlay").click(function (event) {...} -
“我在 Chrome、IE 或 Edge 中似乎没有遇到这个问题” - 因为这只是 Microsoft 的另一种方式“工作” 通过引入一些任意全局变量 (
event) - 这已被 Chrome 采用...
标签: javascript