问题
All of my HTML and CSS code works fine, and my script is properly linked (or so I assume), but I can't get the script to load or do any actions I've coded. Here's my HTML, CSS, and JS code:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Highlights</title>
<link rel='stylesheet' type='text/css' href='testwebcss.css'/>
<script type='text/javascript' src='testwebjs.js'></script>
</head>
<body>
<div id="title" class="highlighted">I'm highlighted!</div>
<div id="text">Highlight me, too!</div>
</body>
</html>
CSS
#title {
background-color: #C02942;
border-radius: 5px;
text-align: center;
font-family: Verdana, Arial, Sans-Serif;
color: #FFFFFF;
width: 200px;
height: 25px;
}
#text {
background-color: #0B486B;
border-radius: 5px;
text-align: center;
font-family: Vivaldi, Cursive;
color: #FFFFFF;
width: 200px;
height: 25px;
opacity: 0.5;
}
.highlighted {
-webkit-box-shadow: 0 0 8px #FFD700;
-moz-box-shadow: 0 0 8px #FFD700;
box-shadow: 0 0 8px #FFD700;
cursor:pointer;
}
JS
$(document).ready(function(){
$('#text').mouseenter(function(){
$(this).fadeTo('fast', 1);
});
$('#text').mouseleave(function(){
$(this).fadeTo('fast', 0.5);
});
});
So I don't understand why it's not working. As you can see, the script simply doubles the opacity when the mouse enters the perimeter of #text. So, why is nothing working. I got this from the console:
Uncaught ReferenceError: $ is not defined
回答1:
Add this to the HTML head (before you include your script):
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
回答2:
You have to add more jQuery
<!DOCTYPE html>
<html>
<head>
<title>Highlights</title>
<link rel='stylesheet' type='text/css' href='testwebcss.css'/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type='text/javascript' src='testwebjs.js'></script>
</head>
<body>
<div id="title" class="highlighted">I'm highlighted!</div>
<div id="text">Highlight me, too!</div>
</body>
</html>
来源:https://stackoverflow.com/questions/20295167/my-jquery-wont-work-on-my-webpage