dojo.NodeList-traverse-- dojo遍历节点列表的操作方法

前端之家收集整理的这篇文章主要介绍了dojo.NodeList-traverse-- dojo遍历节点列表的操作方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

dojo.NodeList-traverse

@H_502_21@ @H_502_21@ @H_502_21@ @H_502_21@
Status: Draft
Version: 1.0
Project owner: James Burke
Available: since 1.4

Method extensions to dojo.NodeList /dojo.query for traversing the DOM. These methods are intended to match the API naming and behavior as the similarly named methods in jQuery.

Introduction

Doing a dojo.require(“dojo.NodeList-traverse”) will add some addition methods to dojo.NodeList (the return object from a dojo.query call) that allow easier traversal of the DOM as it relates to the nodes in the dojo.NodeList.

@H_404_61@

Usage

Here is a simple example showing how dojo.NodeList-traverse adds a “children” method to dojo.NodeList that can be called via the normal method chaining done with a dojo.query result:

@H_502_21@
1
2
3
4
5
6
@H_404_61@
dojo
.
require
(
"dojo.NodeList-traverse"
);


//Grabs all child nodes of all divs

//and returns a dojo.NodeList object

//to allow further chaining operations

dojo
.
query
(
"div"
).
children
();

@H_404_61@
@H_404_61@ @H_404_61@

Methods added by dojo.NodeList-traverse

children

Returns all immediate child elements for nodes in this dojo.NodeList. Optionally takes a query to filter the child elements.

.end() can be used on the returned dojo.NodeList to get back to the original dojo.NodeList.

Example

Assume a DOM created by this markup:

@H_502_21@
1
2
3
4
5
6
7
@H_404_61@
<div
 class=
"container"
>

  <div
 class=
"red"
>
Red One</div>

  Some Text
  <div
 class=
"blue"
>
Blue One</div>

  <div
 class=
"red"
>
Red Two</div>

  <div
 class=
"blue"
>
Blue Two</div>

</div>

@H_404_61@
@H_404_61@
@H_502_21@
dojo
.
require
(
"dojo.NodeList-traverse"
);


//This code returns the four child divs in a dojo.NodeList:

dojo
.
query
(
".container"
).
children
();


//This code returns the two divs that have the class "red" in a dojo.NodeList:

dojo
.
query
(
".container"
).
children
(
".red"
);

@H_404_61@
@H_404_61@ @H_404_61@

closest

Returns closest parent that matches query, including current node in this dojo.NodeList if it matches the query. Optionally takes a query to filter the closest nodes.

.end() can be used on the returned dojo.NodeList to get back to the original dojo.NodeList.

Example

Assume a DOM created by this markup:

@H_502_21@
1
2
3
4
@H_404_61@
dojo
.
require
(
"dojo.NodeList-traverse"
);


//This code returns the div with class "container" in a dojo.NodeList:

dojo
.
query
(
".red"
).
closest
(
".container"
);

@H_404_61@
@H_404_61@ @H_404_61@

parent

Returns immediate parent elements for nodes in this dojo.NodeList. Optionally takes a query to filter the parent elements.

.end() can be used on the returned dojo.NodeList to get back to the original dojo.NodeList.

Example

Assume a DOM created by this markup:

@H_502_21@
1
2
3
4
5
6
@H_404_61@
<div
 class=
"container"
>

  <div
 class=
"red"
>
Red One</div>

  <div
 class=
"blue first"
><span
 class=
"text"
>
Blue One</span></div>

  <div
 class=
"red"
>
Red Two</div>

  <div
 class=
"blue"
><span
 class=
"text"
>
Blue Two</span></div>

</div>

@H_404_61@
@H_404_61@
@H_502_21@
dojo
.
require
(
"dojo.NodeList-traverse"
);


//This code returns the two divs with class "blue" in a dojo.NodeList:

dojo
.
query
(
".text"
).
parent
();


//This code returns the one div with class "blue" and "first" in a dojo.NodeList:

dojo
.
query
(
".text"
).
parent
(
".first"
);

@H_404_61@
@H_404_61@ @H_404_61@

parents

Returns all parent elements for nodes in this dojo.NodeList. Optionally takes a query to filter the parent elements.

.end() can be used on the returned dojo.NodeList to get back to the original dojo.NodeList.

Example

Assume a DOM created by this markup:

@H_502_21@
dojo
.
require
(
"dojo.NodeList-traverse"
);


//This code returns the two divs with class "blue" and the div with class "container" in a dojo.NodeList:

dojo
.
query
(
".text"
).
parents
();


//This code returns the one div with class "container" in a dojo.NodeList:

dojo
.
query
(
".text"
).
parents
(
".first"
);

@H_404_61@
@H_404_61@ @H_404_61@

siblings

Returns all sibling elements for nodes in this dojo.NodeList. Optionally takes a query to filter the sibling elements.

.end() can be used on the returned dojo.NodeList to get back to the original dojo.NodeList.

Example

Assume a DOM created by this markup:

@H_502_21@
<div
 class=
"container"
>

  <div
 class=
"red"
>
Red One</div>

  Some Text
  <div
 class=
"blue first"
>
Blue One</div>

  <div
 class=
"red"
>
Red Two</div>

  <div
 class=
"blue"
>
Blue Two</div>

</div>

@H_404_61@
@H_404_61@
@H_502_21@
1
2
3
4
5
6
7
8
@H_404_61@
dojo
.
require
(
"dojo.NodeList-traverse"
);


//This code returns the two div with class "red" and the other div

//with class "blue" that does not have "first". in a dojo.NodeList:

dojo
.
query
(
".first"
).
siblings
();


//This code returns the two div with class "red" in a dojo.NodeList:

dojo
.
query
(
".first"
).
siblings
(
".red"
);

@H_404_61@
@H_404_61@ @H_404_61@