了解Document.createElement() [英] Understanding Document.createElement()
问题描述
我正在使用 GWT
及其底层 DOM
功能。
I'm using GWT
and its underlaying DOM
capabilities.
我基本上试图实现的是:
What I'm basically trying to achieve is :
- 有一个
div
元素持有一些文本 - 其中一些文本将被
span
元素 - span元素可以互相拖动并提供上下文菜单
- 新的
span
元素可以由最终用户动态创建
- Have a
div
element holding some text - Some of these text would be surrounded by
span
elements - The span elements are draggable into each other and offers contextual menus
- New
span
elements can be created dynamically by the end-users
这可能是这样的:
This is what it could look like :
在应用程序启动时,在最终用户动态创建跨度
期间,我必须做一些元素
和节点
操纵(创建,插入,修改,删除)。为了实现这一点,我必须通过DOM树来找到特定的元素。
At the startup of the application, and during end-users dynamic creation of spans
, I have to do some Element
and Nodes
manipulations (creating, inserting, modifying, deleting). To achieve this, I have to go through the DOM tree to be able to find particular elements.
我正在寻找减少在启动应用程序,在那里我构建我的 div
元素(包含所有文本和 span
元素)
I'm looking for ways to reduce useless time spent at the startup of the application, where I build my div
element (containing all the text and span
elements).
以下示例:
DivElement outermostDiv = Document.get().createDivElement();
processText(outermostDiv, text); // text could be a Java String element
turnTheseIntoSpans(listOfSpans, outermostDiv); // listOfSpans could be a list of text that must be surrounded by span elements.
我们可以想象, turnTheseIntoSpans
做很多使用以下方法修改 expandingDiv
元素: appendChild()
, removeFromParent()
,...
Let's imagine, that turnTheseIntoSpans
do lots of modifications of the outermostDiv
element using methods like : appendChild()
, removeFromParent()
, ...
我的问题是:
My questions are :
-
在将其插入到DOM之前修改 worstDiv 及其子代是一个好习惯吗?
Is it a good practise to modify outermostDiv and its childs before inserting it into the DOM ?
我可以通过 DOM c
expandingDiv
添加到DOM之前,我也想了解一下可浏览的元素树的存在。
I can have access to outermostDiv
childs, sibling of childs, without having added it to the DOM
. I would like to understand how a browsable tree of elements exists even before outermostDiv
is added to the DOM ?
推荐答案
Document.createDivElement()
创建一个实现 com.google.gwt.dom.client.Node
,通过调用以下JavaScript:
Document.createDivElement()
creates an object that implements com.google.gwt.dom.client.Node
, by calling the following JavaScript:
return doc.createElement('div');
这样的一个节点最初没有附加到文档树上,但即使在它之前,你也可以执行大部分操作(除了那些需要其父节点的操作,因为它仍然为空)。
Such a node is not initially attached to the document tree, but even before it is, you can already perform most operations on it (except for the ones that would need its parent node, as this is still null).
注意:节点由相同的文档创建将会被附加(这是必要的,因为由不同文档创建的节点可能不兼容 - 所以你不能总是附加所有节点)。
Note: The node is created by the same document it will later be attached to (this is necessary, because nodes created by a different document may be incompatible - so you can't always attach all nodes everywhere).
这篇关于了解Document.createElement()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!