I want to know if it's possible to set a CSS class e.g. ".duck" as a variable in javascript using jquery e.g. var duck = $(".duck");
我想知道是否可以设置CSS类,例如“.duck”作为javascript使用jquery的变量,例如var duck = $(“。duck”);
I've done this with the "body" tag and with CSS ID's but it doesn't seem to work with any kind of a class.
我用“body”标签和CSS ID做了这个,但它似乎不适用于任何类。
I want to set it as a variable for later use, I have the ID "#frog" set as "var frog = $("#frog");" so in my javascript I can type
我想将它设置为变量供以后使用,我将ID“#frog”设置为“var frog = $(”#frog“);”所以在我的javascript中我可以输入
frog.moveTo(0);
frog.speed(0.3);
frog.autoBounceOff(true);
But for the duck class I still have to use
但对于鸭子类我仍然需要使用
$(".duck").moveTo(270);
$(".duck").speed(0.65);
$(".duck").onCollision(function(otherObj){
Is it possible to set classes as a variable?
是否可以将类设置为变量?
1 个解决方案
#1
2
There is nothing related to CSS here.
这里没有与CSS有关的东西。
-
duck
is an HTML class. -
.duck
is a class selector. -
$(".duck")
is a function call that returns a jQuery object (containing all the DOM elements, in the document, that are members of the class at the time the function is called).
duck是一个HTML类。
.duck是一个类选择器。
$(“。duck”)是一个函数调用,它返回一个jQuery对象(包含文档中的所有DOM元素,这些元素在调用函数时是该类的成员)。
You can store the return value of a function call in a variable.
您可以将函数调用的返回值存储在变量中。
A common convention is to store jQuery objects in variables with names starting with a $
.
常见的约定是将jQuery对象存储在名称以$开头的变量中。
var $duck = $(".duck");
#1
2
There is nothing related to CSS here.
这里没有与CSS有关的东西。
-
duck
is an HTML class. -
.duck
is a class selector. -
$(".duck")
is a function call that returns a jQuery object (containing all the DOM elements, in the document, that are members of the class at the time the function is called).
duck是一个HTML类。
.duck是一个类选择器。
$(“。duck”)是一个函数调用,它返回一个jQuery对象(包含文档中的所有DOM元素,这些元素在调用函数时是该类的成员)。
You can store the return value of a function call in a variable.
您可以将函数调用的返回值存储在变量中。
A common convention is to store jQuery objects in variables with names starting with a $
.
常见的约定是将jQuery对象存储在名称以$开头的变量中。
var $duck = $(".duck");