简体   繁体   English

了解Document.createElement()

[英]Understanding Document.createElement()

I'm using GWT and its underlaying DOM capabilities. 我正在使用GWT及其底层DOM功能。

What I'm basically trying to achieve is : 我基本上想要实现的是:

  • Have a div element holding some text 有一个div元素持有一些文本
  • Some of these text would be surrounded by span elements 其中一些文本将被span元素包围
  • The span elements are draggable into each other and offers contextual menus span元素可以相互拖放,并提供上下文菜单
  • New span elements can be created dynamically by the end-users 最新用户可以动态创建新的span元素

This is what it could look like : 这就是它的样子:

这就是它的样子

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). 在应用程序启动时,以及在最终用户动态创建spans ,我必须进行一些ElementNodes操作(创建,插入,修改,删除)。 To achieve this, I have to go through the DOM tree to be able to find particular elements. 为了实现这一点,我必须通过DOM树才能找到特定的元素。

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). 我正在寻找方法来减少在应用程序启动时花费的无用时间,在那里我构建我的div元素(包含所有text和span元素)。

Take this example : 举个例子:

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.

Let's imagine, that turnTheseIntoSpans do lots of modifications of the outermostDiv element using methods like : appendChild() , removeFromParent() , ... 让我们想象一下, turnTheseIntoSpans使用如下方法对outermostDiv元素进行了大量修改: appendChild()removeFromParent() ,...

My questions are : 我的问题是:

  1. Is it a good practise to modify outermostDiv and its childs before inserting it into the DOM ? 在将outermostDiv及其子项插入DOM之前修改它是一个好习惯吗?

  2. I can have access to outermostDiv childs, sibling of childs, without having added it to the DOM . 我可以访问outermostDiv孩子,孩子的兄弟,而无需将其添加到DOM I would like to understand how a browsable tree of elements exists even before outermostDiv is added to the DOM ? 我想了解在将outermostDiv添加到DOM之前,如何存在可浏览的元素树?

Document.createDivElement() creates an object that implements com.google.gwt.dom.client.Node , by calling the following JavaScript: Document.createDivElement()通过调用以下JavaScript创建一个实现com.google.gwt.dom.client.Node的对象:

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). 注意:节点由稍后将附加到的同一文档创建(这是必要的,因为由不同文档创建的节点可能不兼容 - 因此您无法始终将所有节点附加到所有节点)。

To qualify this question, I must first admit that I am a pure front-ender – I haven't played with GWT and I write raw Javascript, so this answer is based on esoteric knowledge of in-browser DOM. 为了限定这个问题,我必须首先承认我是一个纯粹的前锋 - 我没有玩GWT并且我编写原始Javascript,所以这个答案是基于对浏览器内DOM的深奥了解。

  1. Yes! 是! Live document DOM manipulation is costly. 实时文档DOM操作成本很高。 Manipulating the DOM before insertion is much quicker, since things like style computations, layout reflow calculations, and DOM mutation events are run just once instead of with every individual modification. 在插入之前操作DOM要快得多,因为样式计算,布局重排计算和DOM突变事件之类的操作只运行一次而不是每次修改。

  2. It exists in the DOM – the DOM simply being the XML manipulation abstraction that was used to create it in the first place – it simply not yet part of the document DOM, and all the additional complications that brings about. 它存在于DOM中 - DOM只是用于创建它的XML操作抽象 - 它还不是文档DOM的一部分,以及所带来的所有其他复杂性。

I think you should use a StringBuilder for your implementation. 我认为你应该使用StringBuilder进行实现。 You can do all the manipulations with the StringBuilder (insert tags, change their position, etc.), and when you are done, you add its content to an HTML widget as a string. 您可以使用StringBuilder进行所有操作(插入标记,更改其位置等),完成后,将其内容作为字符串添加到HTML小部件中。 It would be much faster than creating elements, appending children, etc. 这比创建元素,追加孩子等要快得多。

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

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