javascript - 很难解决的问题!:: TypeError: player.loadVideoById 不是函数
问题描述
当$('#myModal').on('show.bs.modal'
被调用。我明白了:
TypeError: player.loadVideoById 不是函数。(在 'player.loadVideoById(x)' 中,'player.loadVideoById' 未定义)
https://jsfiddle.net/rdbj3ty1/5/
在 JS Fiddle 中,代码可以工作,但是当我将它添加到我的代码库时,它会停止工作。我有很多它必须干扰的其他 js 代码和 CSS 代码。有没有办法防止干扰?也许用块包装我的 js 代码或添加更具体的引用?命名空间是问题吗?如果是这样,我该怎么做?
我真的不知道该尝试什么。我在这上面花了 2 天时间,却一无所获。
JS
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'novideoid',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
$('.open-popup').click(function() {
event.target.playVideo();
});
$('.close-popup').click(function(e) {
player.stopVideo();
});
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if(event.data === 0) {
$('.close.close-popup').click();
}
}
function stopVideo() {
player.stopVideo();
}
$(function () {
onYouTubeIframeAPIReady();
$('#myModal').on('show.bs.modal', function(e) {
var videoId = $(e.relatedTarget).data('video-id');
var x = new String(videoId);
player.loadVideoById(x);
});
});
解决方案
推荐阅读
- linux-kernel - linux内核图像字符串提取
- powerbi - 为桑基图创建输入
- laravel - 流明抛出错误:未捕获的错误:在 /var/www/bootstrap/app.php:5 中找不到类 'Laravel\Lumen\Bootstrap\LoadEnvironmentVariables'
- r - 避免数字向量移位数字中的重复
- python - 在烧瓶应用程序中将熊猫表拆分为多个选项卡组
- python - 为什么我们要把for循环的迭代范围写成range(0,len(list)),而不是range(0,len(list)-1)
- c# - 是否可以打开 WPF 应用程序并将参数从电子邮件中的超链接传递给它?
- symfony - 获取不删除资源的集合 API-Platform
- google-cloud-platform - Google 云免费层第二个实例
- python - 多类神经网络为所有类输出相等的可能性