javascript 在页面的 ajax 加载部分初始化 Video.js 播放器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11035946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize a Video.js player on a ajax loaded part of the page
提问by Jeff B.
The video player actually loads fine. My actual problem is when I refresh some parts of my page with AJAX and those parts contains a video player, the HTML5 player loads fine, but not the Video.jspart that customizes it.
视频播放器实际上加载良好。我的实际问题是当我使用 AJAX 刷新页面的某些部分并且这些部分包含视频播放器时,HTML5 播放器加载正常,但不是自定义它的Video.js部分。
The video.js file is loaded in the header of the page. I have read the doc and can't find how to initialize a video player on a page that has already been loaded. Isn't there a myPlayer.initialize()
kind of function I can call when my part of page containing the video is loaded to make the video player load correctly with Video.js?
video.js 文件加载在页面的标题中。我已阅读文档,但找不到如何在已加载的页面上初始化视频播放器。myPlayer.initialize()
当加载包含视频的页面部分以使视频播放器正确加载 Video.js 时,是否没有一种我可以调用的函数?
I think the video.js file does it automatically only on page load.
我认为 video.js 文件仅在页面加载时自动执行。
Thanks for your precious help!
感谢您的宝贵帮助!
回答by Macumbaomuerte
I had the same problem. My scenario:
我有同样的问题。我的场景:
Loading thru ajax a html code with the video tag, it works the first time, but when I reaload the content with ajax (not refreshing the page) it doesn't work.
通过 ajax 加载带有视频标签的 html 代码,它第一次工作,但是当我用 ajax 重新加载内容(不刷新页面)时,它不起作用。
What I did was to switch to finding the video thru the class, since the ID becomes changed by videojs.
我所做的是切换到通过课程查找视频,因为 videojs 更改了 ID。
So this is my call now:
所以这是我现在的电话:
videojs(document.getElementsByClassName("video-js")[0], {}, function(){
// Player (this) is initialized and ready.
});
Hope this helps someone with the same problem where Brunosolution didn't worked.
希望这可以帮助遇到同样问题的人,而布鲁诺解决方案不起作用。
回答by Bruno
Yes, according to the docs you can do:
是的,根据文档,您可以执行以下操作:
videojs("example_video_1", {}, function(){
// Player (this) is initialized and ready.
});
It's specifically to be used with dynamic content :)
它专门用于动态内容:)
Source: http://docs.videojs.com/docs/guides/setup.html(at the bottom)
回答by Сергей Андреевич
I did it in a loop
我是循环做的
var massVideo = $('.video-js');
for(var i = 0; i < massVideo.length; i++){
videojs(massVideo[i]).ready(function(){});
}