所以問題是,當我的html中有腳本時,它可以工作,但當它在外部文件中時,它不能工作。 請幫助我,我找不到正確的答案。
-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="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css">
<link rel="shortcut icon" href="/img/Algebra-logo.png" type="image/x-icon">
<script src="/js/app.js"></script>
<link rel="stylesheet" type="text/css" href="app.css">
<title>SUPIT</title>
</head>
<body>
<div class="topnav">
<a href="#login" class="prijavi-se"><i class="fa fa-sign-in fa-sign-in"></i><span> Prijavi se</span></a>
<a href="/index.html"> <i class="fa fa-home"></i> Po?etna</a>
<a href="#onama"> <i class="fa fa-comments"></i> O nama</a>
<a href="#novosti"><i class="fa fa-circle-info"></i> Novosti</a>
<a href="#kontakt"><i class="fa fa-envelope"></i> Kontakt</a>
</div>
<div class="video-container">
<video src="video/typingOnlaptop.mp4" autoplay loop></video>
<div class="video-text">
<p id="typing"></p>
</div>
</div>
</body>
</html>
射流研究…
$(document).ready(function(){
<script>
const typingElement = document.getElementById("typing");
const textArray = ["Welcome to my website!"];
let arrayIndex = 0;
let characterIndex = 0;
function type(){
if (characterIndex < textArray[arrayIndex].length) {
typingElement.textContent += textArray[arrayIndex].charAt(characterIndex);
characterIndex++;
};
setTimeout(type, 100);
if (characterIndex === textArray[arrayIndex].length) {
setTimeout();
}
}
function erase() {
if (characterIndex > 0) {
typingElement.textContent = textArray[arrayIndex].substring(0, characterIndex - 1);
characterIndex--;
};
setTimeout(erase, 50);
if (characterIndex === 0) {
arrayIndex++;
if (arrayIndex >= textArray.length) {
arrayIndex = 0;
}
setTimeout(type, 500);
}
}
setTimeout(type, 1500);
</script>
});
我用$(文檔)試過。ready(function(){ code line but and otherwise but it not works。我正在努力,但是我不能解決它。
在帶有單獨文件的js中,您不能添加& lt腳本& gt標簽。您應該刪除$(文檔)。ready(function(){ and remove & lt;腳本& gt標簽。只需在& lt/body & gt;標簽。
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="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css">
<link rel="shortcut icon" href="/img/Algebra-logo.png" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="app.css">
<title>SUPIT</title>
</head>
<body>
<div class="topnav">
<a href="#login" class="prijavi-se"><i class="fa fa-sign-in fa-sign-in"></i><span> Prijavi se</span></a>
<a href="/index.html"> <i class="fa fa-home"></i> Po?etna</a>
<a href="#onama"> <i class="fa fa-comments"></i> O nama</a>
<a href="#novosti"><i class="fa fa-circle-info"></i> Novosti</a>
<a href="#kontakt"><i class="fa fa-envelope"></i> Kontakt</a>
</div>
<div class="video-container">
<video src="video/typingOnlaptop.mp4" autoplay loop></video>
<div class="video-text">
<p id="typing"></p>
</div>
</div>
<script src="/js/app.js"></script>
</body>
</html>
JS:
const typingElement = document.getElementById("typing");
const textArray = ["Welcome to my website!"];
let arrayIndex = 0;
let characterIndex = 0;
function type() {
if (characterIndex < textArray[arrayIndex].length) {
typingElement.textContent += textArray[arrayIndex].charAt(characterIndex);
characterIndex++;
}
setTimeout(type, 100);
if (characterIndex === textArray[arrayIndex].length) {
setTimeout();
}
}
function erase() {
if (characterIndex > 0) {
typingElement.textContent = textArray[arrayIndex].substring(
0,
characterIndex - 1
);
characterIndex--;
}
setTimeout(erase, 50);
if (characterIndex === 0) {
arrayIndex++;
if (arrayIndex >= textArray.length) {
arrayIndex = 0;
}
setTimeout(type, 500);
}
}
setTimeout(type, 1500);
JS里不應該有標簽。刪除JS文件中的腳本標簽,它就可以工作了(除非代碼中的其他地方也有問題)
如果您想將javascript文件鏈接到html文件,您應該使用 html標記體中的腳本標記。您應該在代碼中使用的src應該是您的文件的名稱:& lt腳本srs = & quotscript.js & quot& gt& lt/script & gt;。這是完整的代碼:
<!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="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css">
<link rel="shortcut icon" href="/img/Algebra-logo.png" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="app.css">
<title>SUPIT</title>
</head>
<body>
<div class="topnav">
<a href="#login" class="prijavi-se"><i class="fa fa-sign-in fa-sign-in"></i><span> Prijavi se</span></a>
<a href="/index.html"> <i class="fa fa-home"></i> Po?etna</a>
<a href="#onama"> <i class="fa fa-comments"></i> O nama</a>
<a href="#novosti"><i class="fa fa-circle-info"></i> Novosti</a>
<a href="#kontakt"><i class="fa fa-envelope"></i> Kontakt</a>
</div>
<div class="video-container">
<video src="video/typingOnlaptop.mp4" autoplay loop></video>
<div class="video-text">
<p id="typing"></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
您應該從代碼中刪除腳本標記,因為這會導致錯誤。 希望這回答了你的問題。
defer使腳本在文檔加載完成后運行:
<script defer src="/js/app.js"></script>