簡體   English   中英

將 .js 文件鏈接到 .html 文件時,JavaScript 不起作用

[英]JavaScript isn’t working when linking .js file to .html file

最近幾周我一直在學習 HTML、CSS 和 javascript。 我以前的所有編碼經驗都是在我的 CS 課程中使用 C++,所以我是前端開發的新手。

我在 HTML 索引頁中寫了一些 javascript 並且功能正常工作。 但是,當我將它添加到外部 .js 文件時,程序停止工作。

這是 index.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/style.css"/>
    <title> Progress Bar </title>
</head>
<body>
    <audio id="music" src="trax/coldWorld.wav" controls></audio>

    <div class="progress_bar" id="progress_bar">
        <div class="progressed" id="progressed"></div>
    </div>
</body>

<script src="/audio.js"></script>

</html>

.js 文件:

    var music = document.getElementById('music');
    var progressed = document.getElementById('progressed');
    var progress_bar = document.getElementById('progress_bar');

    music.ontimeupdate = function(e) {
        progressed.style.width = Math.floor(music.currentTime *100 / music.duration) + "%";
    }

    progress_bar.onclick = function(e) {
        music.currentTime = ((e.offsetX / progress_bar.offsetWidth) * music.duration);
    }

您可以嘗試將 script 標簽移動到 body 標簽內並更正相對路徑

<!DOCTYPE html>
<html lang="en">
<head>
    ...
</head>
<body>
    ...
    <script src="./audio.js"></script>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM