My jQuery Won't Work on my Webpage

假如想象 提交于 2019-12-25 19:46:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!