Uncaught ReferenceError: $ is not defined problem
Uncaught ReferenceError: $ is not defined problem
我试过将从下面的代码笔站点复制的代码粘贴到保管库中,但它不起作用。我想知道为什么它不起作用。我必须做什么才能让它发挥作用? CSS 源代码是使用 Viw 编译导入的。这重要吗?
码笔地址
https://codepen.io/dodozhang21/pen/vNOmrv
错误:
249 Uncaught ReferenceError: $ is not defined
它说这是问题所在。
var $form = $("#imageUploadForm"),
粘贴源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
@import url(https://fonts.googleapis.com/css?family=Nunito);
@import url(//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css);
*,
*:before,
*:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html {
height: 100%;
}
body {
background: #ffd16e;
height: 100%;
padding: 0;
margin: 0;
font-size: 16px;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.uploadWrapper {
font-family: "Nunito", sans-serif;
}
.imageUploadForm {
background: #6e95f7;
height: 400px;
width: 500px;
position: relative;
display: -webkit-flex;
display: flex;
-webkit-align-items: flex-end;
align-items: flex-end;
-webkit-justify-content: center;
justify-content: center;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
}
.imageUploadForm .helpText {
color: white;
display: block;
position: absolute;
top: 2%;
left: 0;
width: 100%;
height: 100%;
text-align: center;
font-size: 30px;
}
.imageUploadForm .helpText:after {
content: "\f067";
font-family: "FontAwesome";
font-size: 150%;
color: rgba(255, 255, 255, 0.5);
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
margin: 4% auto auto auto;
width: 50%;
height: 50%;
border: 6px dashed rgba(255, 255, 255, 0.5);
}
.imageUploadForm .pickFile {
position: absolute;
bottom: 0;
left: 0;
display: block;
background: white;
height: 25%;
width: 100%;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.imageUploadForm .pickFileButton {
display: inline-block;
padding: 0.7em 2em;
color: white;
background: #fb92ae;
text-decoration: none;
}
.imageUploadForm .pickFileButton:hover {
text-decoration: none;
}
.imageUploadForm .uploadButton {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
z-index: 10;
}
.imageUploadForm.loading .helpText {
font-size: 0;
top: 7%;
}
.imageUploadForm.loading .helpText:before {
font-size: 30px;
content: "Uploading...";
}
.imageUploadForm.loading .helpText:after {
display: none;
}
.imageUploadForm.loading .uploadedImg {
position: absolute;
bottom: 12.5%;
left: 12.5%;
width: 75%;
height: 65%;
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
border: 4px solid white;
-moz-transition: opacity ease-out;
-o-transition: opacity ease-out;
-webkit-transition: opacity ease-out;
transition: opacity ease-out;
}
.imageUploadForm.loading .unveil {
position: absolute;
background: #6e95f7;
display: block;
width: 100%;
height: 100%;
top: 0;
left: 0;
-moz-animation: toTop 4s forwards;
-webkit-animation: toTop 4s forwards;
animation: toTop 4s forwards;
}
.imageUploadForm.loading .unveil:after {
content: "";
position: absolute;
left: -3%;
bottom: 0;
height: 10px;
width: 106%;
background: #ffd16e;
border-radius: 5px;
}
.imageUploadForm.loading .pickFile,
.imageUploadForm.loading .uploadButton {
display: none;
}
.imageUploadForm.loading.loaded {
height: 200px;
}
.imageUploadForm.loading.loaded .uploadedImg {
opacity: 0;
}
.imageUploadForm.loading.loaded .helpText:before {
content: "Upload Complete!";
}
.imageUploadForm.loading.loaded .helpText:after {
display: block;
opacity: 0;
font-size: 0;
line-height: 100px;
-moz-animation: fadeIn 0.4s forwards;
-webkit-animation: fadeIn 0.4s forwards;
animation: fadeIn 0.4s forwards;
content: "\f058";
color: white;
margin-top: 2%;
border-width: 0;
}
@-moz-keyframes toTop {
to {
height: 0;
}
}
@-webkit-keyframes toTop {
to {
height: 0;
}
}
@keyframes toTop {
to {
height: 0;
}
}
@-moz-keyframes fadeIn {
to {
opacity: 1;
font-size: 90px;
}
}
@-webkit-keyframes fadeIn {
to {
opacity: 1;
font-size: 90px;
}
}
@keyframes fadeIn {
to {
opacity: 1;
font-size: 90px;
}
}
</style>
<body>
<div class="uploadWrapper">
<form id="imageUploadForm" class="imageUploadForm">
<span class="helpText" id="helpText">Upload an image</span>
<input type='file' id="file" class="uploadButton" accept="image/*" />
<div id="uploadedImg" class="uploadedImg">
<span class="unveil"></span>
</div>
<span class="pickFile">
<a href="#" class="pickFileButton">Pick file</a>
</span>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$uploadedImg[0].style.backgroundImage = "url(" + e.target.result + ")";
};
reader.readAsDataURL(input.files[0]);
}
}
var $form = $("#imageUploadForm"),
$file = $("#file"),
$uploadedImg = $("#uploadedImg"),
$helpText = $("#helpText");
$file.on("change", function () {
readURL(this);
$form.addClass("loading");
});
$uploadedImg.on(
"webkitAnimationEnd MSAnimationEnd oAnimationEnd animationend",
function () {
$form.addClass("loaded");
}
);
$helpText.on(
"webkitAnimationEnd MSAnimationEnd oAnimationEnd animationend",
function () {
setTimeout(function () {
$file.val("");
$form.removeClass("loading").removeClass("loaded");
}, 5000);
}
);
</script>
</form>
</div>
</body>
</html>
我认为是因为您必须导入 Jquery。
将其放入您的 HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
我试过将从下面的代码笔站点复制的代码粘贴到保管库中,但它不起作用。我想知道为什么它不起作用。我必须做什么才能让它发挥作用? CSS 源代码是使用 Viw 编译导入的。这重要吗?
码笔地址 https://codepen.io/dodozhang21/pen/vNOmrv
错误:
249 Uncaught ReferenceError: $ is not defined
它说这是问题所在。
var $form = $("#imageUploadForm"),
粘贴源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
@import url(https://fonts.googleapis.com/css?family=Nunito);
@import url(//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css);
*,
*:before,
*:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html {
height: 100%;
}
body {
background: #ffd16e;
height: 100%;
padding: 0;
margin: 0;
font-size: 16px;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.uploadWrapper {
font-family: "Nunito", sans-serif;
}
.imageUploadForm {
background: #6e95f7;
height: 400px;
width: 500px;
position: relative;
display: -webkit-flex;
display: flex;
-webkit-align-items: flex-end;
align-items: flex-end;
-webkit-justify-content: center;
justify-content: center;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
}
.imageUploadForm .helpText {
color: white;
display: block;
position: absolute;
top: 2%;
left: 0;
width: 100%;
height: 100%;
text-align: center;
font-size: 30px;
}
.imageUploadForm .helpText:after {
content: "\f067";
font-family: "FontAwesome";
font-size: 150%;
color: rgba(255, 255, 255, 0.5);
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
margin: 4% auto auto auto;
width: 50%;
height: 50%;
border: 6px dashed rgba(255, 255, 255, 0.5);
}
.imageUploadForm .pickFile {
position: absolute;
bottom: 0;
left: 0;
display: block;
background: white;
height: 25%;
width: 100%;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.imageUploadForm .pickFileButton {
display: inline-block;
padding: 0.7em 2em;
color: white;
background: #fb92ae;
text-decoration: none;
}
.imageUploadForm .pickFileButton:hover {
text-decoration: none;
}
.imageUploadForm .uploadButton {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
z-index: 10;
}
.imageUploadForm.loading .helpText {
font-size: 0;
top: 7%;
}
.imageUploadForm.loading .helpText:before {
font-size: 30px;
content: "Uploading...";
}
.imageUploadForm.loading .helpText:after {
display: none;
}
.imageUploadForm.loading .uploadedImg {
position: absolute;
bottom: 12.5%;
left: 12.5%;
width: 75%;
height: 65%;
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
border: 4px solid white;
-moz-transition: opacity ease-out;
-o-transition: opacity ease-out;
-webkit-transition: opacity ease-out;
transition: opacity ease-out;
}
.imageUploadForm.loading .unveil {
position: absolute;
background: #6e95f7;
display: block;
width: 100%;
height: 100%;
top: 0;
left: 0;
-moz-animation: toTop 4s forwards;
-webkit-animation: toTop 4s forwards;
animation: toTop 4s forwards;
}
.imageUploadForm.loading .unveil:after {
content: "";
position: absolute;
left: -3%;
bottom: 0;
height: 10px;
width: 106%;
background: #ffd16e;
border-radius: 5px;
}
.imageUploadForm.loading .pickFile,
.imageUploadForm.loading .uploadButton {
display: none;
}
.imageUploadForm.loading.loaded {
height: 200px;
}
.imageUploadForm.loading.loaded .uploadedImg {
opacity: 0;
}
.imageUploadForm.loading.loaded .helpText:before {
content: "Upload Complete!";
}
.imageUploadForm.loading.loaded .helpText:after {
display: block;
opacity: 0;
font-size: 0;
line-height: 100px;
-moz-animation: fadeIn 0.4s forwards;
-webkit-animation: fadeIn 0.4s forwards;
animation: fadeIn 0.4s forwards;
content: "\f058";
color: white;
margin-top: 2%;
border-width: 0;
}
@-moz-keyframes toTop {
to {
height: 0;
}
}
@-webkit-keyframes toTop {
to {
height: 0;
}
}
@keyframes toTop {
to {
height: 0;
}
}
@-moz-keyframes fadeIn {
to {
opacity: 1;
font-size: 90px;
}
}
@-webkit-keyframes fadeIn {
to {
opacity: 1;
font-size: 90px;
}
}
@keyframes fadeIn {
to {
opacity: 1;
font-size: 90px;
}
}
</style>
<body>
<div class="uploadWrapper">
<form id="imageUploadForm" class="imageUploadForm">
<span class="helpText" id="helpText">Upload an image</span>
<input type='file' id="file" class="uploadButton" accept="image/*" />
<div id="uploadedImg" class="uploadedImg">
<span class="unveil"></span>
</div>
<span class="pickFile">
<a href="#" class="pickFileButton">Pick file</a>
</span>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$uploadedImg[0].style.backgroundImage = "url(" + e.target.result + ")";
};
reader.readAsDataURL(input.files[0]);
}
}
var $form = $("#imageUploadForm"),
$file = $("#file"),
$uploadedImg = $("#uploadedImg"),
$helpText = $("#helpText");
$file.on("change", function () {
readURL(this);
$form.addClass("loading");
});
$uploadedImg.on(
"webkitAnimationEnd MSAnimationEnd oAnimationEnd animationend",
function () {
$form.addClass("loaded");
}
);
$helpText.on(
"webkitAnimationEnd MSAnimationEnd oAnimationEnd animationend",
function () {
setTimeout(function () {
$file.val("");
$form.removeClass("loading").removeClass("loaded");
}, 5000);
}
);
</script>
</form>
</div>
</body>
</html>
我认为是因为您必须导入 Jquery。
将其放入您的 HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>