3 回答

TA貢獻1863條經驗 獲得超2個贊
您的文件應該在 html 文件中調用庫script.js之后調用。jquery
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="js/script.js"></script>
$(document).ready(function() {
$(".collapse-control").click(function() {
$(".collapse-box").slideUp();
$(this).closest(".collapse").find(".collapse-box").slideDown();
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Lesson</title>
<meta name="viewport" content="width=vice-width, initial-scale=1">
<meta name="author" content="Unknown">
<meta name="description" content="description">
<meta name="keywords" content="web,html,css,html5,development">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<div class="collapse">
<h2 class="collapse-control">Open box 1</h2>
<div class="collapse-box">
This is box 1
</div>
</div>
</body>
</html>

TA貢獻1809條經驗 獲得超8個贊
當您在包含腳本后包含 jQuery 時,這是沒有意義的。
您應該在包含 jQuery 之后包含您的腳本。
所以你的代碼將是這樣的:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Lesson</title>
<meta name="viewport" content="width=vice-width, initial-scale=1">
<meta name="author" content="Unknown">
<meta name="description" content="description">
<meta name="keywords" content="web,html,css,html5,development">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<div class="collapse">
<h2 class="collapse-control">Open box 1</h2>
<div class="collapse-box">
This is box 1
</div>
</div>
</body>
</html>

TA貢獻1963條經驗 獲得超6個贊
嘗試使用:
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="js/script.js"></script>
已安裝:
<script src="js/script.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
希望能幫助到你。
- 3 回答
- 0 關注
- 222 瀏覽
添加回答
舉報