如何从另一个javascript文件调用videojs对象

在我的主 html 文件 (index.html) 中,我在标题中导入了 videojs javascript 文件。在正文中,我已准备好 videojs 播放器。

我想要的是使用另一个 javascript 文件中的 videojs 播放器对象,例如 set playrate, .

但是当我编码它时,我想显示 id,它说未定义并且我收到了一个脚本错误。

我在 Visual Studio 2019 工作

希望有人能给我一些线索。

最好的问候克拉斯扬

我得到的错误是:提供的元素或 ID 无效。(videojs)


HUWWW
浏览 213回答 2
2回答

慕哥6287543

<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="utf-8" />&nbsp; &nbsp; <title>CadenceVR.com BTLE example</title>&nbsp; &nbsp; <link href="https://vjs.zencdn.net/7.6.5/video-js.css" rel="stylesheet">&nbsp; &nbsp; <link rel="icon" sizes="192x192" href="../favicon.png">&nbsp; &nbsp; <script>window.HELP_IMPROVE_VIDEOJS = false;</script>&nbsp; &nbsp; <script type="text/javascript" src='https://vjs.zencdn.net/7.6.5/video.js'></script>&nbsp; &nbsp; <script type="text/javascript" src="heartRateSensor.js"></script>&nbsp; &nbsp; <script type="text/javascript" src="cyclingSpeedCadence.js"></script>&nbsp; &nbsp; <script type="text/javascript" src="core.js"></script></head><body>&nbsp; &nbsp; <div id="container">&nbsp; &nbsp; &nbsp; &nbsp; <div id="statusText">Heart&nbsp; &nbsp;: 0 BPM &#x2764;</div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="SpeedText">Speed&nbsp; &nbsp;:&nbsp; 0,0 KM/H</div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="CadenceText">Cadence :&nbsp; 0 RPM</div>&nbsp; &nbsp; &nbsp; &nbsp; <label for="name">Speedrate between 1 and 4:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="speedrate" name="speedrate" required minlength="1" maxlength="1" size="2">&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" name="BTLEbutton1" value="BTLEClick1" onclick="onButtonClickSetVideoRate();">Set Video speed rate</button>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" name="BTLEbutton2" value="BTLEClick2" onclick="onButtonClickHart();">Discover Heart Devices</button>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" name="BTLEbutton3" value="BTLEClick3" onclick="onButtonClickCSC();">Discover S_C Devices</button>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp; &nbsp; <video id='cvrvideo' class='video-js' controls preload="auto"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;poster='My-jpg' height="320" width="640" data-setup='{}'>&nbsp; &nbsp; &nbsp; &nbsp; <source src='My-mp4' type='video/mp4'>&nbsp; &nbsp; &nbsp; &nbsp; <p class='vjs-no-js'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; To view this video please enable JavaScript, and consider upgrading to a web browser that&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href='https://videojs.com/html5-video-support/' target='_blank'>supports HTML5 video</a>&nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; </video>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;videojs('cvrvideo').ready(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Store the video object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var myPlayer = this, id = myPlayer.id();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Make up an aspect ratio&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var aspectRatio = 264/640;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function resizeVideoJS(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var width = document.getElementById(id).parentElement.offsetWidth;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myPlayer.width(width);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myPlayer.height(width * aspectRatio);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Initialize resizeVideoJS()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resizeVideoJS();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Then on resize call resizeVideoJS()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.onresize = resizeVideoJS;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; </script></body></html>

鸿蒙传说

两种情况:在您设置 PlayRate 之前,视频 Js 对象尚未初始化。在这种情况下,您应该在 body 标记之后移动导入脚本你只想使用Video Js Object,你可以使用全局有价值的。在 index.html 中window['videojsPlayer']&nbsp;=&nbsp;new&nbsp;videojsPlayer();在另一个文件中。window['videojsPlayer'].setPlayrate()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript