使用 jQuery 访问 img 标签 src
我可以这样做吗?
$("#myImage"+1).attr("src", "path/to/newImage.jpg");
因为图像就像一个数组,所以 img 标签的 id 是 myImage1
<img src="path/to/oldImage.jpg" id="myImage1">
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">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以像这样迭代图像元素:
Yes, you could iterate round image elements like this:
是的。你可以这么做。
jQuery 选择器只是字符串。 jQuery 并不关心如何将字符串组合在一起。它不知道,它不在乎,它永远不会问问题。只要您完成的字符串是有效的选择器(一旦您像示例中那样将其连接起来就是有效的选择器),jQuery 就会知道该怎么做。
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.
是的,这是 jQuery 的有效代码:) 它与使用 Javascript 进行串联相同。
Yes that's valid code for jQuery :) It is same as you do the concatenation with Javascript.
是的,你可以。
发帖之前先试一下不是更容易吗?会花更少的时间...
Yes you can.
Isn't it easier to try it before posting? it would take less time...
您可以为 jQuery 对象添加下标,例如数组
或更详细的 jQuery
get(0)
。但是,如果您只是构建 jQuery 选择器的字符串,那么当然可以。 jQuery 不知道该字符串是如何生成的(或者它关心),只要它是一个有效的选择器即可。
You can subscript jQuery objects like an array
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.