gpt4 book ai didi

javascript - 美元符号未定义

转载 作者:行者123 更新时间:2023-12-01 03:13:43 42 4
gpt4 key购买 nike

我用谷歌搜索了答案,但还没有一个合适的。我是网站 build 的新手。我正在构建一个选项卡功能,可以更改页面上的内容(代码 ex 来自 https://codepen.io/cssjockey/pen/jGzuK )

当我在我的身上尝试时。我收到一条错误消息 '$' is undefined 。这是我的代码

index.html

<!DOCTYPE html>
<html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Site</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />
<script language="JavaScript" src="functions.js"></script>
</head>
<body>
<div class="container">

<ul class="tabs">
<li class="tab-link current" data-tab="tab-1">TabA</li>
<li class="tab-link" data-tab="tab-2">TabB</li>
<li class="tab-link" data-tab="tab-3">TabC</li>
<li class="tab-link" data-tab="tab-4">TabD</li>
</ul>

<div id="tab-1" class="tab-content current">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<div id="tab-2" class="tab-content">
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div id="tab-3" class="tab-content">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
</div>
<div id="tab-4" class="tab-content">
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>

functions.js

$(document).ready(function(){

$('ul.tabs li').click(function(){
var tab_id = $(this).attr('data-tab');

$('ul.tabs li').removeClass('current');
$('.tab-content').removeClass('current');

$(this).addClass('current');
$("#"+tab_id).addClass('current');
})

})

感谢您提供的任何帮助!

最佳答案

这是因为美元符号 ( $ ) 代表 jQuery library usage.

要将其包含在您的项目中,只需从其 CDN 导入即可:

<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>

总而言之,您的 <head>标签应如下所示:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Site</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script language="JavaScript" src="functions.js"></script>
</head>

关于javascript - 美元符号未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45665842/

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