jQuery类选择器不起作用,而id选择器仅适用于"body"对象. [英] jQuery class selector doesn't work and id selector works only with "body"

查看:245
本文介绍了jQuery类选择器不起作用,而id选择器仅适用于"body"对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jquery应用CSS值,但是由于某些原因,类选择器或id选择器不起作用.

I'm trying to apply CSS values with jquery but class selector or id selector does not not work for some reason.

这是我的小提琴:

如您所见,如果我使用id ="kitten",则什么也不会发生.

As you can see nothing happens if I use id="kitten".

HTML

    <div id="kitten"></div>

脚本

    $(function(){
    var x = 0;
    setInterval(function(){
        x-=1;
        $("kitten").css("background-position", x + "px 0");
    }, 10);
});

CSS

    html, body { height: 100%; width: 100%;}
    #kitten {
    background-image: url("http://sereedmedia.com/srmwp/wp-content/upload/kitten.jpg");
    background-repeat: repeat-x;
    }

它不起作用.但是,如果我使用"body"而不是id,则一切都会开始工作.

It does not work. However if I use "body" instead of id - all starts to work.

用小提琴身体"

为什么我不能使用jQuery id选择器或类选择器?

Why I cannot use jQuery id selector or class selector?

推荐答案

首先,您需要执行以下操作:

First you need do:

$("#kitten").css("background-position", x + "px 0");

因为您没有获得ID选择器.

Becouse you are not getting the Id selector.

并为div定义宽度和高度,以显示图像,如下所示:

And define a width and height to your div, to show your image like:

#kitten {
    background-image: url("http://sereedmedia.com/srmwp/wp-content/uploads/kitten.jpg");
    background-repeat: repeat-x;
    width: 390px; <----- LIKE this
    height: 200px; <----- LIKE this
}

演示

这篇关于jQuery类选择器不起作用,而id选择器仅适用于"body"对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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