未捕获的ReferenceError:$未定义

Mas*_*uel 8 javascript jquery image referenceerror

我在尝试创建JQuery图像滑块时收到此错误消息.

未捕获的ReferenceError:$未定义

这是我的新编码(请注意,我已将脚本移至页面,这是adobe建议的.):

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Green Cold-Formed Steel | Home</title>
<style type="text/css">

body,td,th {
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 10px;
    color: #000;
    text-align: left;
}
body {
    background-color: #999;
}
a:link {
    color: #999;
    text-decoration: none;
}
a:visited {
    text-decoration: none;
    color: #060;
}
a:hover {
    text-decoration: underline;
    color: #FFF;
}
a:active {
    text-decoration: none;
}
h1 {
    font-size: 14px;
    color: #060;
}
h2 {
    font-size: 12px;
    color: #999;
}
h3 {
    font-size: 9px;
    color: #FFF;
}
#next {
    background-image: url(Assets/slideshow/r_arrow.png);
    background-repeat: no-repeat;
    background-position: center center;
    display: block;
    float: right;
    height: 500px;
    width: 100px;
    position: relative;
    z-index: 99;
}
#slideshowwrapper {
    display: block;
    height: 500px;
    width: 1000px;
    margin: auto;
}
#container {
    background-color: #FFC;
    display: block;
    float: left;
    height: 500px;
    width: 1000px;
    overflow: auto;
}
#prev {
    background-image: url(Assets/slideshow/L_arrow.png);
    background-repeat: no-repeat;
    background-position: center center;
    display: block;
    float: left;
    height: 500px;
    width: 100px;
    position: relative;
    z-index: 99;
}
#slider {
    display: block;
    float: left;
    height: 500px;
    width: 1000px;
    overflow: hidden;
    position: absolute;
}
#NavBar {
    display: block;
    height: 50px;
    width: auto;
    position: relative;
    padding-bottom: 5px;
    float: none;
    vertical-align: middle;
}
</style>
</head>

<body bgcolor="#999999" text="#000000">

<table width="100%" height="583" border="0" cellspacing="0" cellpadding="0px">
  <tr>
    <th height="132" align="left" scope="col">&nbsp;</th>
    <th scope="col"><div class="spacer" id="spacer"></div>
      <div class="Header" id="header"><a href="index.html"><img src="Assets/Logo/GFCS_Logo_NoBckgnd.png" width="288" height="108" alt="GCFS"></a></div>
    <hr></th>
    <th scope="col">&nbsp;</th>
  </tr>
  <tr>
    <th width="5%" align="left" scope="col">&nbsp;</th>
    <td width="85%" align="left" valign="top" scope="col">
    <div class="Navigation Bar" id="NavBar"><img src="Assets/navigation/navbutton_static_green.png" width="100" height="50" alt="Navi_Home"> <img src="Assets/navigation/navbutton_static_green.png" width="100" height="50" alt="Navi_Solutions"> <img src="Assets/navigation/navbutton_down.png" width="100" height="50" alt="navi_down"></div>

    <div id="slideshowwrapper">
      <div id="container">
        <div class="controller" id="prev"></div>
        <div id="slider">
            <img src="Assets/slideshow/hyatt_apts.png" width="1000" height="500" alt=           "Hyatt Apartments">
            <img src="Assets/slideshow/mccommas.png" width="1000" height="500" alt="McCommas">
            <img src="Assets/slideshow/park_4200.png" width="1000" height="500" alt="Park 4200">
            <img src="Assets/slideshow/quail_run.png" width="1000" height="500" alt="Quail Run">
            <img src="Assets/slideshow/roofdale.png" width="1000" height="500" alt="Roofdale Roof">
            <img src="Assets/slideshow/tri_truss.png" width="1000" height="500"> </div>
        <div class="controller" id="next"></div>
      </div>
    </div></td>
    <th width="10%" scope="col">&nbsp;</th>
  </tr>
</table>
<p>&nbsp;</p>
<script type="text/javascript" src="JS/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="JS/jquery.cycle.all.js"></script>
<script>
$(function(){
        $('#slider').cycle({ 
        fx:     'scrollHorz', 
        speed:  'fast', 

        next:   '#next', 
        prev:   '#prev' 
    });
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的网站(本地)的JS文件夹的文件夹路径如下所示:C:\ Users\Andrew\Desktop\GCFS\JS

我是编码世界的新手,但我想要实现的目标非常简单.据我所知,我不需要有一个就绪函数,javascript应该自动运行.谢谢您的帮助!

来自评论

当我尝试这个时它也失败了

<title>Green Cold-Formed Steel | Home</title> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="JS/jquery.cycle.all.js"></script> 
<script type="text/javascript"> 
 $(function() {  
   $('#slider').cycle({ fx: 'scrollHorz', speed: 'fast', next: '#next', prev: '#prev' });
}); 
</script>
Run Code Online (Sandbox Code Playgroud)

wri*_*wan 12

解决方案:

1)使用Google CDN加载jQuery

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

2)就我所知的Cycle Plugin而言,它与jQuery v1.5-v1.7兼容,因为大多数方法都在jQuery 1.8+中被弃用.这就是我在第1点中使用了Google CDN jquery的v1.5的原因.循环插件中的大多数示例都使用jquery 1.5.

3)清除你的浏览器缓存,这是大多数时候的主要罪魁祸首.

4)使用下面的代码检查jquery的加载

if(typeof jQuery!=='undefined'){
    console.log('jQuery Loaded');
}
else{
    console.log('not loaded yet');
}
Run Code Online (Sandbox Code Playgroud)

主要编辑:

错误的原因很简单:

在加载jquery之前调用了循环方法.

DOM上的调用循环插件准备好了..

$(document).ready(function(){
    $('#slider').cycle({ 
        fx:     'scrollHorz', 
        speed:  'fast', 

        next:   '#next', 
        prev:   '#prev' 
    });  
});
Run Code Online (Sandbox Code Playgroud)


Sus*_* -- 9

2个问题.

看起来你的jQuery没有正确加载.

您通常会看到此错误

Uncaught ReferenceError:$ is not defined
Run Code Online (Sandbox Code Playgroud)

当jQuery未正确包含在您的页面上时.

尝试使用CDN中的jQuery,它应该可以解决您的问题

更换

<script src="JS/jquery-1.10.1.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

与来自cdn的那个

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

注意:如果从文件系统进行测试,则需要将其添加http:到上述URL,否则将失败

接下来你的脚本文件在HTML.之前.所以需要在DOM Ready处理程序中包含代码.

$(function() {
    $('#slider').cycle({ 
        fx:     'scrollHorz', 
        speed:  'fast', 
        next:   '#next', 
        prev:   '#prev' 
    });
});
Run Code Online (Sandbox Code Playgroud)

据我所知,当我创建div Id时,会引用"Slider".

不它不是.如果您的脚本刚好包含在正文之前,那么您可能不会将它包含在Ready处理程序中.但在你的情况下它存在于头部.因此,当脚本开始运行时,该特定元素仍然不存在DOM

检查小提琴