简体   繁体   English

未捕获ReferenceError:未定义$? 的jsfiddle

[英]Uncaught ReferenceError: $ is not defined? Jsfiddle

I'm a beginner at javascript and i have used jsfiddle to create a navigation bar which appears when the user has scrolled down. 我是javascript的初学者,我使用jsfiddle创建了一个导航栏,当用户向下滚动时出现。

When i copy the code to dreamweaver it no longer works? 当我将代码复制到Dreamweaver时,它不再起作用?

I have researched and it said something about adding the jquery framework or something? 我已经研究过,它说了有关添加jquery框架的内容或其他内容? Or is there a way to do this without the framework? 还是没有框架就可以做到这一点?

Link to jsfiddle for full code: http://jsfiddle.net/fNn7K/270/ 链接到jsfiddle以获取完整代码: http : //jsfiddle.net/fNn7K/270/

javascript : javascript:

$(window).on('scroll', function () {
  console.log($(this).scrollTop());
   if ($(this).scrollTop() > 50) {
     $('.nav').addClass('visible');

   }else if ($(this).scrollTop() <= 50 && $('.nav').hasClass('visible')) {
      $('.nav').removeClass('visible');
   }

});

Without jQuery you can do : 没有jQuery,您可以执行以下操作:

window.onscroll = function() {
    var display = document.body.scrollTop > 150 ? 'inline' : 'none',
        elems   = document.getElementsByTagName('nav');

    for (var i=0; i<elems.length; i++) {
        elems[i].style.display = display;
    }
}

FIDDLE 小提琴

When i copy the code to dreamweaver it no longer works? 当我将代码复制到Dreamweaver时,它不再起作用?

JS Fiddle assembles a page based on several pieces of user entered data. JS Fiddle根据几条用户输入的数据组装页面。 One of those pieces of data is the selection of a library. 这些数据之一是选择库。

You have to copy the code to the right places in the document and include the same libraries. 您必须将代码复制到文档中的正确位置,并包含相同的库。

Even then, the preview modes of Dreamweaver might not show it up, because they are (or at least were) entirely awful. 即使那样,Dreamweaver的预览模式也可能无法显示,因为它们(或至少是)非常糟糕。 Do you testing in a real browser. 您是否在真实的浏览器中进行测试?

I have researched and it said something about adding the jquery framework or something? 我已经研究过,它说了有关添加jquery框架的内容或其他内容?

You need the jQuery library to use jQuery methods, yes. 您需要jQuery库才能使用jQuery方法。

Or is there a way to do this without the framework? 还是没有框架就可以做到这一点?

jQuery is just some JavaScript written by other people. jQuery只是其他人编写的一些JavaScript。 You can reproduce anything it does. 您可以复制它所做的任何事情。 A line by line rewrite of your code to not use jQuery would be out of scope for a stackoverflow answer though. 但是,逐行重写代码以不使用jQuery超出了stackoverflow答案的范围。

you need to add jquery.js file in your code (dreamweaver).. 您需要在代码中添加jquery.js文件(dreamweaver)。

add this in between <head> tag <head>标签之间添加

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

in the fiddle you provided, the jquery is already loaded..so you didn't get that error. 在您提供的小提琴中,jquery已经加载了..所以您没有收到该错误。

and don't forget to wrap your code inside document.ready function (which is again, already added in fiddle).. 并且不要忘了将代码包装在document.ready函数中(同样,已经在小提琴中添加了)。

 $(function(){
     $(window).on('scroll', function () {
       .....
     });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM