MDN wants to learn about developers like you:

tree是用来显示表格或层级行元素的容器。 包含若干行 (rows )和列(columns)。树的每行包含若干缩进显示的子行(child rows)。 和其他元素不同的是,用来显示的数据不是通过标签(tags),而是通过一个显示对象(view object)来确定的。这个显示对象(view object)实现了接口 nsITreeView . The view is queried for the data to appear in the tree. 树的使用有以下几种方法, 第二列列出了通过树的 view属性(property)的可用的接口. 第三列指示treeitem 元素是否使用.

If you would like to allow the tree to be horizontally scrolled, simply set the width attributes for each column to make the tree wider than its containing object.

Tree type View interfaces Has DOM nodes? Description
Content tree nsITreeView, nsITreeContentView Yes This tree has treeitem elements placed within the treechildren element. In this situation, a content view (which implements the interface nsITreeContentView) which is a more specialized type of view, uses the treeitem elements and their descendants to determine the data to display in the tree. However, the treeitems are not displayed directly; they are used only as data to the content view. However, the content view will automatically update the tree if treeitems are changed.
RDF tree nsITreeView, nsIXULTreeBuilder No This tree is generated from an RDF datasource. It is used when a tree has a datasources attribute, and has dont-build-content in its flags attribute. For this tree, the data comes directly from the RDF datasource. DOM treeitems are not created. Even though the template uses treeitem elements to define the content, DOM nodes for these elements are not created. This is the type that should be used for RDF generated trees with lots of rows.
RDF content tree nsITreeView, nsIXULTreeBuilder, nsITreeContentView Yes This tree is generated from an RDF datasource. It is similar to the previous type but is used when the tree does not have dont-build-content in its flags attribute. DOM treeitems are created, so you can access the data using RDF functions or DOM functions. This type is suitable for RDF generated trees with a fairly small number of rows.
Custom tree view nsITreeView No For this tree you implement the nsITreeView interface yourself. The tree's data is retrieved from this custom view. The custom view should be attached to the tree by setting its view property.

More information is available in the XUL tutorial. Also Tree Widget Changes.

Relevant accessbility guidelines
  • Provide alternative access (e.g., via menus) to column picker and for header behaviors like sorting (these have no default keyboard access).
disableKeyNavigation, disabled, editable, enableColumnDrag, flags, hidecolumnpicker, onselect, rows, seltype, statedatasource, tabindex, treelines
accessibleType, builderView, columns, contentView, currentIndex, disableKeyNavigation, disabled, editingColumn, editingRow, enableColumnDrag, firstOrdinalColumn, inputField, selType, selstyle, tabIndex, treeBoxObject, view


A tree with several columns

<tree flex="1" rows="2">

    <treecol id="sender" label="Sender" flex="1"/>
    <treecol id="subject" label="Subject" flex="2"/>

        <treecell label=""/>
        <treecell label="Top secret plans"/>
        <treecell label=""/>
        <treecell label="Let's do lunch"/>


A tree with several columns and nested items

<tree id="myTree" flex="1" hidecolumnpicker="false" seltype="single" class="tree"
  <treecols id="myTree2-treeCols">
    <treecol id="myTree2-treeCol0" primary="true" flex="2" label="Column A"
             persist="width" ordinal="1"/>
    <splitter class="tree-splitter" ordinal="2"/>
    <treecol id="myTree2-treeCol1" flex="1" label="Column B"
             persist="width" ordinal="3"/>
        <treecell label="1"/>
        <treecell label="a"/>
    <!-- Make sure to set container="true" -->
    <treeitem container="true" open="true">
        <treecell label="2"/>
        <treecell label="b"/>
            <treecell label="2a"/>
            <treecell label="ba"/>






// Disabling an element
document.getElementById('buttonRemove').setAttribute("disabled", "true");

// Enabling back an element by removing the "disabled" attribute

Firefox 3.5 note

For keyset elements, support for this attribute was added in Firefox 3.5.

Type: boolean
If this attribute is not used, the user can navigate to specific items within the element by pressing keys corresponding to letters in the item's label. This is done incrementally, so typing more letters with select more specific items. This feature may be disabled by setting this attribute to true.
Type: boolean
Indicates that the cells of the tree may be edited.
Type: boolean
When set to true, the user may drag the column headers around to change the order in which they are displayed.
Type: space-separated list of the values below
A set of flags used for miscellaneous purposes. Two flags are defined, which may be the value of this attribute.
  • dont-test-empty: For template generated content, the builder will not check that a container is empty.
  • dont-build-content: This flag may be used on a tree to indicate that content elements should not be generated. This results in a performance enhancement, but you will not be able to use the DOM functions to retrieve the tree rows.
Type: boolean
When set to false, a drop-down will appear in the upper right corner of the tree, which the user may use to show and hide columns. When set to true, the column picker will be hidden. The default value is false.
Type: script code
This event is sent to a tree when a row is selected, or whenever the selection changes. The user can select multiple rows by holding down Shift or Control and clicking on a row. The onselect event will be sent for each item added to or removed from the selection.
Type: integer
The number of rows to display in the element. If the element contains more than this number of rows, a scrollbar will appear which the user can use to scroll to the other rows. To get the actual number of rows in the element, use the getRowCount method.
Type: one of the values below
Used to indicate whether multiple selection is allowed.
Only one row may be selected at a time. (Default in listbox and richlistbox.)
Multiple rows may be selected at once. (Default in tree.)

For trees, you can also use the following values:

Individual cells can be selected
Rows are selected; however, the selection highlight appears only over the text of the primary column.

For richlistbox, this is new in Firefox 3.5.

Type: URI
Chrome XUL may specify an RDF datasource to use to store tree state information. This is used to hold which tree items are open and which items are collapsed. This information will be remembered for the next time the XUL file is opened. If you do not specify this attribute, state information will be stored in the local store (rdf:local-store).
Type: integer
The tab order of the element. The tab order is the order in which the focus is moved when the user presses the "tab" key. Elements with a higher tabindex are later in the tab sequence.

Type: boolean
When set to true, lines are drawn connecting the lines in the tree; when false, the lines are not drawn. This is false by default.


Type: integer
A value indicating the type of accessibility object for the element.

Type: nsIXULTreeBuilder
A reference to the tree builder which constructed the tree data. The builder provides access to the RDF resources for each row in the tree, and allows sorting the data by column. In newer versions of Mozilla, the builderView property is actually a synonym for the view property, since the two interfaces are flattened together into a single interface in JavaScript. This property is read-only.
Type: nsITreeColumns
Returns the columns for the tree as an nsITreeColumns object.
Type: nsITreeContentView
For trees built with a content builder - that is, those that do not have flags set -- the contentView will be a reference to the nsITreeContentView for the tree. This interface lets you retrieve the DOM element corresponding to a given a row index and vice versa. For trees that are not built with a content builder, the functions of nsITreeContentView will not be available, since there are no DOM nodes to retrieve. In newer versions of Mozilla, the contentView property is actually a synonym for the view property, since the two interfaces are flattened together into a single interface in JavaScript. This property is read-only.
Type: integer

Set to the row index of the tree caret in the tree. For trees with focus, the caret's position is indicated by the focus ring, but unfocused trees won't show  a focus ring, naturally. For unfocused trees, the (undrawn) caret's position can still be obtained by this property. If the caret isn't present for any row (for example, because the tree has never been focused), the value will be -1.

You cannot rely on this property to change or determine a tree selection, except for trees with seltype="single". (All trees have seltype="multiple" by default.) To reliably change or determine a selection, instead use the nsITreeSelection interface methods available via tree.view.selection.

Type: boolean
Gets and sets the value of the disabled attribute.

Type: boolean
Gets or sets the value of the disableKeyNavigation attribute.
Type: nsITreeColumn
The column of the tree cell currently being edited, or null if there is no cell being edited.
Type: integer
The row index of the tree cell currently being edited, or -1 if there is no cell currently being edited.
Type: boolean
When set to true, the user may drag the column headers around to change the order in which they are displayed.
Type: treecol element
A reference to the first treecol element, which or may not be the first column displayed in the tree.
Type: textbox element
Read-only property that holds the textbox that is used for editing.

Type: string
Gets and sets the value of the seltype attribute.


Type: string
If set to the value primary, only the label of the primary column will be highlighted when an item in the tree is selected. Otherwise, the entire row will be highlighted. To see the difference, compare the selection style in the folder list and the message list in Mozilla mail.
Type: integer
Gets and sets the value of the tabindex attribute.

Type: nsITreeBoxObject
The box object is responsible for rendering the tree on the window. This object implements the nsITreeBoxObject interface and contains functions for retrieving the cells at certain coordinates, redrawing cells and scrolling the tree. This property is equivalent to the boxObject property.
Type: nsITreeView
The view for the tree, which is the object which generates the data to be displayed. You can assign an object which implements nsITreeView to this property. Trees built from RDF or those which use treeitems directly will already have a view. Functions available in the view allow one to retrieve the data within the cells, and determine which rows are nested within others. For a complete list of view functions, see the nsITreeView interface.


Inherited from XUL element
blur, click, doCommand, focus, getElementsByAttribute

Inherited from DOM element
addEventListener(), appendChild(), dispatchEvent(), getAttribute(), getAttributeNode(), getAttributeNodeNS(), getAttributeNS(), getElementsByTagName(), getElementsByTagNameNS(), hasAttribute(), hasAttributeNS(), hasAttributes(), hasChildNodes(), insertBefore(), isSupported(), normalize(), removeAttribute(), removeAttributeNode(), removeAttributeNS(), removeChild(), removeEventListener(), replaceChild(), setAttribute(), setAttributeNode(), setAttributeNodeNS(), setAttributeNS()

startEditing( row, column )
Return type: no return value
Activates user editing of the given cell, which is specified by row index number and the nsITreeColumn in which it is located. The tree view's nsITreeView.getCellText() method is called to obtain the cell contents.
stopEditing( shouldaccept )
Return type: no return value
Stops editing the cell currently being edited. If the shouldAccept parameter is true, the cell's label is changed to the edited value (the tree view's nsITreeView.setCellText() method is called to change the cell contents). Otherwise the cell label is reverted to the value it had prior to editing.

treecols, treecol, treechildren, treeitem, treerow, treecell and treeseparator.
nsIAccessibleProvider, nsIDOMXULTreeElement, nsIDOMXULMultiSelectControlElement

Script Examples

To have alternating colors for each row, use the style rules like the following: pma at daffodil dot uk dot com

treechildren::-moz-tree-row(selected) { background-color: #FFFFAA; }
treechildren::-moz-tree-row(odd) { background-color: #EEEEEE; }
treechildren::-moz-tree-row(odd, selected) { background-color: #FFFFAA; }
treechildren::-moz-tree-cell-text(selected) { color: #000000; }
treechildren::-moz-tree-cell-text(odd, selected) { color: #000000; }

If using a content tree view, use the following to get the value of the id attribute for each of the selected rows of a tree: tcooper_mont at yahoo dot com

var idList = [];
var rangeCount = tree.view.selection.getRangeCount();
for (var i = 0; i < rangeCount; i++)
   var start = {};
   var end = {};
   tree.view.selection.getRangeAt(i, start, end);
   for(var c = start.value; c <= end.value; c++)

The following returns a array of the indicies of the rows where the value is checked in a checkbox type column: jfabre at ismans dot fr

function getCellChecked(tree, columnid)
  var arr = [];
  var column = tree.columns.getNamedColumn(columnid);
  for (var i = 0; i < tree.view.rowCount; i++){
    if (tree.view.getCellValue(i, column) == 'true')
  return arr;

To get the text value for a specific column (e.g. column 'age') from the currently focused row in the tree:

var t = document.getElementById('mytree');
document.title = t.view.getCellText(t.currentIndex,t.columns.getNamedColumn('age'));


 此页面的贡献者: Lingchar, ziyunfei, Mhoudg
 最后编辑者: Lingchar,