jQuery代码不工作 [英] jQuery code not working

查看:71
本文介绍了jQuery代码不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,但对编程并不陌生。我仍然无法找出这段简单的代码有什么问题:

  $(document).ready(function(){
$('。footer')。click(function(){
$('。footer')。fadeOut('slow');
});
});

它不适用于我自己的网站,但它在我使用codeacademy时可以正常工作。
这有什么问题?


编辑:前一段时间,我提出了这个问题。当我开始使用jQuery时,这是一件很简单的事情,当时我并不明白。解决的办法是在JavaScript中采取任何其他操作之前先导入库。代码在codeacedemy中工作,因为库自动导入。这对每个初学者都很重要,但是这个信息无处不在网络上。

解决方案

几个检查点...



确保包含jQuery库,位于< head> 内。

 <脚本SRC = http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js >< /脚本> 

如果没问题,是否有类 .footer ?如果不是,它是 ID ?在这种情况下,您需要添加以下内容:

  $(document).ready(function(){
$ ('#footer')。click(function(){
$('#footer')。fadeOut('slow');
});
});

实际上,这可以以另一种最佳方式完成:

  $(document).ready(function(){
$('#footer')。click(function(){
$ ).fadeOut('slow');
});
});


I'm new to jQuery, but not new to programming. Still I cannot find out whats wrong with this simple piece of code:

$(document).ready(function() {
    $('.footer').click(function() {
        $('.footer').fadeOut('slow');
    });
});

It won't work on my own website, but it does work when I use codeacademy. What is wrong with this?

Edit: Awhile ago I made this question. It was a simple thing I did not understand back then when I started jQuery. The solution was to import the library first before taking any other actions in javascript. The code did work in codeacedemy because the library is automatically imported. This is very important to know for each beginner, but this piece of infomation is everywhere on the web.

解决方案

Few checkpoints...

Make sure you include the jQuery library, inside your <head>.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

If that's fine, do you have an element with the class .footer? If not, is it an ID? In that case, you need to add this:

$(document).ready(function() {
    $('#footer').click(function() {
        $('#footer').fadeOut('slow');
    });
});

Actually, this can be done in another best way:

$(document).ready(function() {
    $('#footer').click(function() {
        $(this).fadeOut('slow');
    });
});

这篇关于jQuery代码不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆