为什么我不能附加一个元素,删除它,然后第二次附加同一个元素? [英] Why can't I append an element, remove it, then append the same element a second time?

查看:17
本文介绍了为什么我不能附加一个元素,删除它,然后第二次附加同一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面提供的代码中,您将看到由各种图块组成的地图.单击磁贴将选择"它.

In the code provided below, you will see a map made of various tiles. Clicking on a tile will "select" it.

我想要发生的事情:

  1. 点击图块
  2. 按下放置"按钮
  3. 那个瓷砖上出现了一个石油井架
  4. 按删除"按钮
  5. 油井架消失了
  6. 点击另一个图块
  7. 按下放置"按钮
  8. 那个瓷砖上出现了一个新的石油井架

程序运行到第 7-8 步.出于某种原因,石油井架没有再次出现.

The program works up until steps 7-8. For some reason, the oil derrick isn't appearing again.

我设置了 PlaceRemove 按钮,以便一次只能在地图上显示一个油井架.一个石油井架,我不希望在移除之前选择带有石油井架的瓷砖(因此,您可以选择不同的瓷砖,单击删除"仍然会移除石油井架,无论它在什么瓷砖上).

I have the Place and Remove buttons set up so that only one oil derrick may be on the map at a time. To remove an oil derrick, I do not expect the tile with the oil derrick on it to be selected before removal (hence, you can have a different tile selected and clicking Remove will still remove the oil derrick no matter what tile it is on).

我认为问题在于石油井架第二次没有正确附加.这可能是因为它被删除的方式.

What I think is the problem is that the oil derrick isn't getting appended correctly the second time. This may be because of the way it's being removed.

http://jsfiddle.net/briz/jdhPW/17/

另一方面,我意识到如果在选择图块之前按下放置"按钮,则存在潜在的错误.

On a side not, I realize that there is a potential bug if the Place button gets pressed before a tile is selected.

推荐答案

有很多方法可以解决这个问题.我认为最简单的方法是 $('.oilDerrick').hide() 而不是 remove().另一种方法是附加克隆并更改类名,但这需要更改更多代码.

There are many ways to fix this. I think the simplest would be to $('.oilDerrick').hide() rather than remove(). An alternative would be to append clones and change class names, that requires changing a bit more code though.

更新: http://jsfiddle.net/jdhPW/19/

这篇关于为什么我不能附加一个元素,删除它,然后第二次附加同一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆