简体   繁体   English

移动DOM元素的方法

[英]Methods to Move DOM Element

I am programming a Javascript game as an exercise in objects and AJAX. 我正在将Javascript游戏编程为对象和AJAX的练习。 It involves movement of wessels around a nautical-themed grid. 它涉及Wessel在航海主题网格周围的运动。 Although the wessels are in an array of objects, I need to manipulate their graphical representation, their sprites. 尽管这些wessel在一个对象数组中,但我需要操纵它们的图形表示,精灵。 At the moment I have chosen, from a DOM perspective, to use 'img' elements within 'td' elements. 从DOM的角度来看,目前我选择在“ td”元素中使用“ img”元素。 From a UI continuity perspective, which method of programmatically moving the elements with Javascript would be recommended: 从UI连续性的角度来看,将推荐使用Javascript以编程方式移动元素的方法:
(a) deleting inner html of 'from' cell (td element) and rewriting inner html of 'to' cell, (a)删除“ from”单元格(td元素)的内部html并重写“ to”单元格的内部html,
(b) clone the img node (sprite), delete the original node from its parent, and append it to the 'to' cell, or (b)克隆img节点(精灵),从其父节点删除原始节点,并将其附加到“至”单元格,或
(c) using positioning relative to the table element for the sprite, ignoring the td's alltogether (although their background [color] represents the ocean depth). (c)使用相对于表格元素的小精灵定位,而忽略td的所有元素(尽管其背景[color]代表海洋深度)。

I would definitely stick with moving the sprite from cell to cell rather than using relative positioning to the table. 我绝对会坚持将精灵从一个单元格移动到另一个单元格,而不是使用相对于表格的相对位置。 My reasoning is that the table cell size might vary from browser to browser (given variances in the way padding, margins etc. are rendered - especially with annoying IE) and calculating the exact location to position the sprite in order for it to line up within a given cell might get complicated. 我的理由是,表格单元格的大小可能会因浏览器而异(给定填充,边距等的呈现方式存在差异-尤其是在烦人的IE中),并计算确切的位置来放置精灵以使其在其中排列给定的单元格可能会变得复杂。

That narrows it down to (a) or (b) for your options. 这将其范围缩小到(a)或(b)供您选择。 Here let's eliminate option (a), as deleting the HTML from inside is not a clean way of manipulating the DOM. 这里我们消除选项(a),因为从内部删除HTML并不是操作DOM的一种干净方法。 I like the idea of storing the node in an object, and then appending it to the 'to' cell, and then deleting the original node, which your option (b) suggests. 我喜欢将节点存储在对象中,然后将其附加到“ to”单元格,然后删除原始节点的想法,您的选择(b)建议这样做。 This way, you are still dealing with the high-level 'objects' and not the low-level 'text' needlessly. 这样,您仍在不必要地处理高级“对象”而不是低级“文本”。 You don't need to mess with the text - for such an application, that would be the dirty 'hackish' way of doing it if you didn't know about the DOM manipulation functions JavaScript already offers. 您无需弄乱文本-对于这样的应用程序,如果您不了解JavaScript已经提供的DOM操作功能,那将是肮脏的“骇人听闻”的处理方式。

My answer is (b). 我的答案是(b)。 However, if you absolutely require speed - though for your game I don't know if you'll really need the extra boost - you may consider option (a). 但是,如果您绝对需要速度-尽管对于您的游戏,我不知道您是否真的需要额外的提升-您可以考虑选择(a)。 A few sources, such as http://www.quirksmode.org/dom/innerhtml.html contend that the DOM manipulation methods are generally slower than using innerHTML. 诸如http://www.quirksmode.org/dom/innerhtml.html之类的一些资料争辩说,DOM操作方法通常比使用innerHTML慢。 But that's the general rule with everything. 但这是所有内容的一般规则。 The lower the level you go, the faster you can make your code. 级别越低,编写代码的速度就越快。 The higher the level, the easier to understand and conceptualize the code is, and in my opinion, since speed will not make a huge difference in this case, keep it neat and go with (b). 级别越高,代码越容易理解和概念化,并且我认为,由于在这种情况下速度不会产生很大变化,因此请使其保持整洁并与(b)保持一致。

There's no need to clone the img node, delete the old one and append the clone. 无需克隆img节点,删除旧节点并追加克隆。 Just append the img node to the receiving td. 只需将img节点附加到接收td。 It will automatically be removed from the td it was previously in. Simple, effective and fast. 它会自动从以前的td中删除。简单,有效和快速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM