gpt4 book ai didi

javascript - 使用 jQuery 创建 Accordion ,但得到 "$ is not defined"

转载 作者:可可西里 更新时间:2023-11-01 13:43:15 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的 Accordion (可折叠)来显示不同的面板,当单击按钮时,但由于某种原因,它不起作用。

我查看了我的 DevTools,发现 $ 在定义之前被使用

$('.accordion').on('click', '.accordion-control', function(e) {
$(this).next('.accordion-panel').not(':animated').slideToggle();
e.preventDefault();
});
<ul class="accordion">
<li>
<button class="accordion-control">Phase one</button>
<div class="accordion-panel">Context will go here</div>
</li>
</ul>

我的脚本标签就在我的结束正文标签之上和我的 jQuery 脚本之下。有人可以帮忙吗?

最佳答案

在加载和定义之前调用 jQuery(它的简写 $)。将您的脚本放在 HTML 正文标记的正确位置(底部),然后将您的 JavaScript 代码放入 jQuery's ready function :


你自己的版本

$(function() {
$('.accordion').on('click', '.accordion-control', function(e) {
$(this).next('.accordion-panel').not(':animated').slideToggle();
e.preventDefault();
});
});
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>My Awesome Page</title>
</head>

<body>
<ul class="accordion">
<li>
<button class="accordion-control">Phase one</button>
<div class="accordion-panel">Context will go here</div>
</li>
</ul>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<!-- Your script or source below. -->
<script src="scripts/my-script.js"></script>
</body>

</html>


使用 Bootstrap

首先,使用下面的这些模板并将 jQuery 放在 HTML 页面的底部,在结束 body 标记之前,但在您的实际脚本和 Bootstrap 脚本之前:

Bootstrap 3

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<title>My Awesome Page</title>
</head>
<body>
<h1>Hello there!</h1>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Your script or source below. -->
<script src="scripts/my-script.js"></script>
</body>
</html>

Accordions in Bootstrap 3

Bootstrap 4

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>My Awesome Page</title>
</head>
<body>
<h1>Hello there!</h1>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- Your script or source below. -->
<script src="scripts/my-script.js"></script>
</body>
</html>

Accordions in Bootstrap 4


然后将您的 JavaScript 代码放入 jQuery 的 ready 中功能:

$(document).ready(function() {
// Your accordion code here
});

使用它的简写版本:

$(function () {
// Your accordion code here
});

ES6 :

$(() => {
// Your accordion code here
});

关于javascript - 使用 jQuery 创建 Accordion ,但得到 "$ is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51004385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com