Ive got a function:
setInterval ( doSomething, 100 );
function doS开发者_运维技巧omething ( )
{
$("#i_msl").animate({ top: "-150px",}, 1000 ).delay(1000);
$("#i_msl").animate({ top: "-300px",}, 1000 ).delay(1000);
$("#i_msl").animate({ top: "0px",}, 1000 ).delay(1000);
}
but it works only for one element. How can I make it work for all elements with #i_msl on page?
You are assigning an ID to multiple elements. You can't do that. Try giving them a class instead:
class="i_msl"
You can access it with $(".i_msl")
You can't. You can only have one unique id
per page. Change it from id
to class
, and target by $('.i_msl')
.
The selector #i_msl
is for a specific ID; these are supposed to be unique on the page. Instead, assign each element a unique class, such as:
class="my-class"
Then just select them using a class selector:
$(".my-class").animate({ top: "-150px",}, 1000 ).delay(1000);
$(".my-class").animate({ top: "-300px",}, 1000 ).delay(1000);
$(".my-class").animate({ top: "0px",}, 1000 ).delay(1000);
#i_msl
is an ID selector. You can (should) only have a single item on the page with a particular id. If you need multiple elements to match the selector, use a class: .i_msl
Also, setInterval
will be called every 100ms
which may cause issues with execution when your animation takes longer than the interval.
精彩评论