jQuery attr() 更改 img src

发布于 2024-11-06 12:36:42 字数 563 浏览 0 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林很绿却致人迷途 2024-11-13 12:36:42

您可以在此处删除原始图像:

newImg.animate(css, SPEED, function() {
    img.remove();
    newImg.removeClass('morpher');
    (callback || function() {})();
});

剩下的就是 newImg。然后,您使用 #rocket 重置链接引用图像:

$("#rocket").attr('src', ...

但是您的 newImg 没有 id 属性,更不用说 id< 火箭的/code>。

要解决此问题,您需要删除 img,然后将 newImgid 属性设置为 rocket

newImg.animate(css, SPEED, function() {
    var old_id = img.attr('id');
    img.remove();
    newImg.attr('id', old_id);
    newImg.removeClass('morpher');
    (callback || function() {})();
});

然后你会再次得到闪亮的黑色火箭:http://jsfiddle.net/ambigously/W2K9D/

更新:更好的方法(如 Mellamokb 所指出的)是隐藏原始图像,然后在按下重置按钮时再次显示它。首先,将重置操作更改为如下所示:

$("#resetlink").click(function(){
    clearInterval(timerRocket);
    $("#wrapper").css('top', '250px');
    $('.throbber, .morpher').remove(); // Clear out the new stuff.
    $("#rocket").show();               // Bring the original back.
});

newImg.load 函数中,抓取图像原始大小:

var orig = {
    width: img.width(),
    height: img.height()
};

最后,完成变形动画的回调变为:

newImg.animate(css, SPEED, function() {
    img.css(orig).hide();
    (callback || function() {})();
});

新增和改进: < a href="http://jsfiddle.net/ambigously/W2K9D/1/" rel="nofollow">http://jsfiddle.net/ambigously/W2K9D/1/

$ 的泄露插件外部的 ('.throbber, .morpher') 并不是最好的事情,但只要有记录就没什么大不了的。

You remove the original image here:

newImg.animate(css, SPEED, function() {
    img.remove();
    newImg.removeClass('morpher');
    (callback || function() {})();
});

And all that's left behind is newImg. Then you reset link references the image using #rocket:

$("#rocket").attr('src', ...

But your newImg doesn't have an id attribute let alone an id of rocket.

To fix this, you need to remove img and then set the id attribute of newImg to rocket:

newImg.animate(css, SPEED, function() {
    var old_id = img.attr('id');
    img.remove();
    newImg.attr('id', old_id);
    newImg.removeClass('morpher');
    (callback || function() {})();
});

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:

$("#resetlink").click(function(){
    clearInterval(timerRocket);
    $("#wrapper").css('top', '250px');
    $('.throbber, .morpher').remove(); // Clear out the new stuff.
    $("#rocket").show();               // Bring the original back.
});

And in the newImg.load function, grab the images original size:

var orig = {
    width: img.width(),
    height: img.height()
};

And finally, the callback for finishing the morphing animation becomes this:

newImg.animate(css, SPEED, function() {
    img.css(orig).hide();
    (callback || function() {})();
});

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.

氛圍 2024-11-13 12:36:42
  1. 函数imageMorph将创建一个新的img元素,因此id被删除。
    更改为

    $("#wrapper > img")

  2. 如果你想再次发射火箭,你应该使用 live() 函数来处理点击事件。

更新的演示:http://jsfiddle.net/ynhat/QQRsW/4/

  1. Function imageMorph will create a new img element therefore the id is removed.
    Changed to

    $("#wrapper > img")

  2. You should use live() function for click event if you want you rocket lanch again.

Updated demo: http://jsfiddle.net/ynhat/QQRsW/4/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文