我有这个用于练习的示例站点,我在其中添加了一个视频,其中包含一些带有 JavaScript 的按钮。当我在 HTML 文件中运行脚本时,它运行得很好,但是如果我试图从 .js 文件中运行它,它就不起作用,并且 bottons 不会做任何事情。有什么想法吗?
html:
<!DOCTYPE html>
<html>
<head>
<title>Blue Developer Directory</title>
<link type="text/css" rel="stylesheet" href="css/stylesheet.css">
<script type="text/javascript" src="js.js"></script>
</head>
<body>
<video id="ad" width="420" height="350" controls>
<source src="video/ad.mp4" type="video/mp4">
Please update your browser
</video>
<div style="width:400px;">
<button onclick="playPause()">Play/Pause</button>
<button onclick="makeBig()">Big</button>
<button onclick="makeSmall()">Small</button>
<button onclick="makeNormal()">Normal</button>
</div>
</body>
Javascript:
var myVideo=document.getElementById(ad);
function playPause(){
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
function makeBig(){
myVideo.width=560;
}
function makeSmall(){
myVideo.width=320;
}
function makeNormal(){
myVideo.width=420;
}