jQuery attr() 更改 img src
我正在用 jQuery 制作一些火箭发射效果。当我单击“火箭”时,它将与另一个火箭图像交换,然后发射。当我单击“重置”链接时,Rocket 必须重置起始位置,并且图像必须恢复原状。但有两个问题。首先,“我的火箭图像不会恢复”。其次 - 在它恢复到初始位置后,如果我再次单击火箭,它不会启动。你能看到并找到我的解决方案吗?
http://jsfiddle.net/thisizmonster/QQRsW/
$("#resetlink").click(function(){
clearInterval(timerRocket);
$("#wrapper").css('top', '250px');
$("#rocket").attr('src', 'http://www.kidostore.com/images/uploads/P-SAM-rocket-blk.jpg');
});
可以看到 $("rocket") .attr() 行。
I'm making some Rocket launching effect by jQuery. When I click on Rocket, it'll swap with another rocket image, and then launch up. When I click "Reset" link, Rocket must reset starting location and image must revert back. But there are two problems. First, "my rocket image is not reverting back". Second - after it's reverted to initial location, if I click on Rocket again, it's not launching. Can you see and find me solutions?
http://jsfiddle.net/thisizmonster/QQRsW/
$("#resetlink").click(function(){
clearInterval(timerRocket);
$("#wrapper").css('top', '250px');
$("#rocket").attr('src', 'http://www.kidostore.com/images/uploads/P-SAM-rocket-blk.jpg');
});
You can see $("rocket").attr() row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此处删除原始图像:
剩下的就是
newImg
。然后,您使用#rocket
重置链接引用图像:但是您的
newImg
没有id
属性,更不用说id<
火箭
的/code>。要解决此问题,您需要删除
img
,然后将newImg
的id
属性设置为rocket
:然后你会再次得到闪亮的黑色火箭:http://jsfiddle.net/ambigously/W2K9D/
更新:更好的方法(如 Mellamokb 所指出的)是隐藏原始图像,然后在按下重置按钮时再次显示它。首先,将重置操作更改为如下所示:
在
newImg.load
函数中,抓取图像原始大小:最后,完成变形动画的回调变为:
新增和改进: < a href="http://jsfiddle.net/ambigously/W2K9D/1/" rel="nofollow">http://jsfiddle.net/ambigously/W2K9D/1/
$ 的泄露插件外部的 ('.throbber, .morpher')
并不是最好的事情,但只要有记录就没什么大不了的。You remove the original image here:
And all that's left behind is
newImg
. Then you reset link references the image using#rocket
:But your
newImg
doesn't have anid
attribute let alone anid
ofrocket
.To fix this, you need to remove
img
and then set theid
attribute ofnewImg
torocket
:And then you'll get the shiny black rocket back again: http://jsfiddle.net/ambiguous/W2K9D/
UPDATE: A better approach (as noted by mellamokb) would be to hide the original image and then show it again when you hit the reset button. First, change the reset action to something like this:
And in the
newImg.load
function, grab the images original size:And finally, the callback for finishing the morphing animation becomes this:
New and improved: http://jsfiddle.net/ambiguous/W2K9D/1/
The leaking of
$('.throbber, .morpher')
outside the plugin isn't the best thing ever but it isn't a big deal as long as it is documented.函数
imageMorph
将创建一个新的img元素,因此id被删除。更改为
$("#wrapper > img")
如果你想再次发射火箭,你应该使用 live() 函数来处理点击事件。
更新的演示:http://jsfiddle.net/ynhat/QQRsW/4/
Function
imageMorph
will create a new img element therefore the id is removed.Changed to
$("#wrapper > img")
You should use live() function for click event if you want you rocket lanch again.
Updated demo: http://jsfiddle.net/ynhat/QQRsW/4/