jQuery class selector doesn't work and id selector works only with “body”

前端 未结 5 651
生来不讨喜
生来不讨喜 2021-01-21 17:32

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

Here is my fiddle:

As you can see nothing happen

5条回答
  •  终归单人心
    2021-01-21 18:14

    Id selectors are prefixed with #:

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

    A class selector would be prefixed with a ., for instance if kitten were a class:

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

    When you use the body selector you are using a tag selector which has no prefix.

    $("input"); //selects all input tags
    

    For most basic selectors, just follow CSS selector syntax.

提交回复
热议问题