定义dom中属性的顺序 [英] Define the order of attributes in dom

查看:37
本文介绍了定义dom中属性的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究DOM,我想知道如何更改标签数据的位置例如

我已经创建了元素:

  propElement = document.createElement("prop"); 

道具正在打开标签.

然后

  propElement.setAttribute("name","name1");propElement.setAttribute("name2","name2"); 

问题是,尽管我将设置方法name2放在name1之后,但仍会在标签name2中看到name1之前.

如何更改订单?

(注意;我使用的是Java DOM API,而不是JavaScript.)

解决方案

您不能,元素上的属性顺序并不重要.实际上,在实时DOM中,没有 .顺序似乎只与DOM的序列化形式有关(例如HTML标记等).即便如此,该命令也没有任何意义,除非与无效文本有关(下文中有更多内容).

属性基本上是对象(附有DOM元素)的简单属性.它们绝对没有顺序,实际上它们在DOM中的表示形式是

请记住,DOM描述了一个对象模型,这一点很重要.DOM的序列化格式可以是文本的(例如,定义DOM的HTML文档),但是DOM不是.在HTML文档中,由于它是线性文本(从上到下,从左到右),因此自然地,定义一个属性的文本必须在描述另一个属性的文本之前,但这并不意味着对结果DOM对象中的属性的一种排序,因为它们根本没有顺序.因此:

 < div a ="1" b ="2"> ...</div> 

精确地描述 相同的元素:

 < div b ="2" a ="1"> ...</div> 

生成的元素是 div ,该元素具有属性 a 和值为 1 的属性 b 值为 2 .

这与在程序源中的对象上设置属性完全相同.考虑一些具有 x y 属性的假设 obj .这段代码:

  obj.a = 1;obj.b = 2; 

...产生与该代码完全相同的对象:

  obj.b = 2;obj.a = 1; 

...提供的 a b 确实是简单的字段(不是可能产生副作用的隐藏函数调用),而DOM中的属性就是这样./p>

以一种简单的方式可以使DOM的文本(序列化)形式的属性顺序有意义,并且仅与无效文本有关:多次指定同一个属性,只使用给定的第一个值,因为多次指定同一个属性是无效的.这些值不会合并,后续值也不会覆盖前一个值.仅使用第一个.

因此,此无效 HTML:

 < div class ="foo" class ="bar"> ...</div> 

...实际上导致具有 class div ("bar" 不是存在).但这只是处理无效序列化表格的一种应对机制.

I currently working on DOM and i wonder how can change the place of tags data for example

I have created element:

propElement = document.createElement("prop");

The prop is opening the tag.

Then

propElement.setAttribute("name", "name1");

propElement.setAttribute("name2", "name2");

The problem is that despite i put the set method name2 after name1 I will see in the tag name2 before name1.

How can I change the order ?

(Note; I'm using a Java DOM API, not JavaScript.)

解决方案

You can't, the order of attributes on elements is not significant. In fact, in a live DOM, there is no order. Order only seems to exist in relation to the serialized form of a DOM (e.g., HTML markup and the like). And even then, the order doesn't have any meaning except in relation to invalid text (more below).

Attributes are basically simple properties of an object (the DOM element to which they're attached). There is absolutely no order to them, and in fact the representation of them in the DOM is a NamedNodeMap which is "...not maintained in any particular order."

It's important to remember that the DOM describes an object model. The serialized form of a DOM may be textual (for instance, an HTML document defining a DOM), but the DOM is not. In an HTML document, since it's linear text (top-to-bottom, left-to-right), naturally the text defining one attribute has to precede the text describing another, but that does not imply any kind of order to the attributes in the resulting DOM object, because they have no order at all. So this:

<div a="1" b="2">...</div>

describes exactly the same element as this:

<div b="2" a="1">...</div>

The resulting element is a div which has an attribute a with the value 1 and an attribute b with the value 2.

This is exactly the same as setting properties on an object in program source. Consider some hypothetical obj with x and y properties. This code:

obj.a = 1;
obj.b = 2;

...results in exactly the same object as this code:

obj.b = 2;
obj.a = 1;

...provided a and b really are simple fields (not hidden function calls that may have side effects), which is true of attributes in the DOM.

There is one small way in which attribute order in the textual (serialized) form of a DOM may be significant, and it's only related to invalid text: If the same attribute is specified more than once, only the first value given is used, because it's invalid to specify the same attribute more than once. The values are not combined, and the subsequent value doesn't overwrite the previous one. The first one, only, is used.

So this invalid HTML:

<div class="foo" class="bar">...</div>

...actually results in a div with class "foo" ("bar" is not present at all). But this is just a coping mechanism for dealing with invalid serialized forms.

这篇关于定义dom中属性的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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