0

Can I do this?

$("#myImage"+1).attr("src", "path/to/newImage.jpg");

Because the image is like an array so the id for the img tag is myImage1

<img src="path/to/oldImage.jpg" id="myImage1">
4

5 回答 5

0

Yes. You can do that.

jQuery selectors are just strings. How you put together the string is really of no concern to jQuery. It doesn't know, it doesn't care, it'll never ask questions. As long as your completed string is a valid selector (which it is once you've concatenated it as in your example), jQuery will know what to do.

于 2010-10-14T13:56:16.840 回答
0

Yes that's valid code for jQuery :) It is same as you do the concatenation with Javascript.

于 2010-10-14T13:56:29.257 回答
0

Yes you can.
Isn't it easier to try it before posting? it would take less time...

于 2010-10-14T13:56:53.280 回答
0

You can subscript jQuery objects like an array

$('a')[0] 

or the more verbose jQuery get(0).

However, if you were just string building a jQuery selector, then yes, of course that will work. jQuery doesn't know how that string was made (or does it care), so long as it is a valid selector.

于 2010-10-14T13:56:59.150 回答
0

是的,您可以像这样迭代圆形图像元素:

for (var i=0;i<=5;i++)
{
 $("myImage" + i).attr("src", "path/to/newImage.jpg"); ?
}
于 2010-10-14T14:00:04.567 回答