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

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

dojo.NodeList-traverse

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.

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:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //Grabs all child nodes of all divs
  10.  
  11. //and returns a dojo.NodeList object
  12.  
  13. //to allow further chaining operations
  14.  
  15. dojo
  16. .
  17. query
  18. (
  19. "div"
  20. ).
  21. children
  22. ();
  23.  

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:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  1. <div
  2. class=
  3. "container"
  4. >
  5.  
  6. <div
  7. class=
  8. "red"
  9. >
  10. Red One</div>
  11.  
  12. Some Text
  13. <div
  14. class=
  15. "blue"
  16. >
  17. Blue One</div>
  18.  
  19. <div
  20. class=
  21. "red"
  22. >
  23. Red Two</div>
  24.  
  25. <div
  26. class=
  27. "blue"
  28. >
  29. Blue Two</div>
  30.  
  31. </div>
  32.  
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the four child divs in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".container"
  16. ).
  17. children
  18. ();
  19.  
  20.  
  21. //This code returns the two divs that have the class "red" in a dojo.NodeList:
  22.  
  23. dojo
  24. .
  25. query
  26. (
  27. ".container"
  28. ).
  29. children
  30. (
  31. ".red"
  32. );
  33.  

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:

  1. 1
  2. 2
  3. 3
  4. 4
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the div with class "container" in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".red"
  16. ).
  17. closest
  18. (
  19. ".container"
  20. );
  21.  

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:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  1. <div
  2. class=
  3. "container"
  4. >
  5.  
  6. <div
  7. class=
  8. "red"
  9. >
  10. Red One</div>
  11.  
  12. <div
  13. class=
  14. "blue first"
  15. ><span
  16. class=
  17. "text"
  18. >
  19. Blue One</span></div>
  20.  
  21. <div
  22. class=
  23. "red"
  24. >
  25. Red Two</div>
  26.  
  27. <div
  28. class=
  29. "blue"
  30. ><span
  31. class=
  32. "text"
  33. >
  34. Blue Two</span></div>
  35.  
  36. </div>
  37.  
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two divs with class "blue" in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".text"
  16. ).
  17. parent
  18. ();
  19.  
  20.  
  21. //This code returns the one div with class "blue" and "first" in a dojo.NodeList:
  22.  
  23. dojo
  24. .
  25. query
  26. (
  27. ".text"
  28. ).
  29. parent
  30. (
  31. ".first"
  32. );
  33.  

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:

  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two divs with class "blue" and the div with class "container" in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".text"
  16. ).
  17. parents
  18. ();
  19.  
  20.  
  21. //This code returns the one div with class "container" in a dojo.NodeList:
  22.  
  23. dojo
  24. .
  25. query
  26. (
  27. ".text"
  28. ).
  29. parents
  30. (
  31. ".first"
  32. );
  33.  

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:

  1. <div
  2. class=
  3. "container"
  4. >
  5.  
  6. <div
  7. class=
  8. "red"
  9. >
  10. Red One</div>
  11.  
  12. Some Text
  13. <div
  14. class=
  15. "blue first"
  16. >
  17. Blue One</div>
  18.  
  19. <div
  20. class=
  21. "red"
  22. >
  23. Red Two</div>
  24.  
  25. <div
  26. class=
  27. "blue"
  28. >
  29. Blue Two</div>
  30.  
  31. </div>
  32.  
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two div with class "red" and the other div
  10.  
  11. //with class "blue" that does not have "first". in a dojo.NodeList:
  12.  
  13. dojo
  14. .
  15. query
  16. (
  17. ".first"
  18. ).
  19. siblings
  20. ();
  21.  
  22.  
  23. //This code returns the two div with class "red" in a dojo.NodeList:
  24.  
  25. dojo
  26. .
  27. query
  28. (
  29. ".first"
  30. ).
  31. siblings
  32. (
  33. ".red"
  34. );
  35.  

nextAll

Returns all sibling elements that come after the 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:

  1. <div
  2. class=
  3. "container"
  4. >
  5.  
  6. <div
  7. class=
  8. "red"
  9. >
  10. Red One</div>
  11.  
  12. Some Text
  13. <div
  14. class=
  15. "blue first"
  16. >
  17. Blue One</div>
  18.  
  19. <div
  20. class=
  21. "red next"
  22. >
  23. Red Two</div>
  24.  
  25. <div
  26. class=
  27. "blue next"
  28. >
  29. Blue Two</div>
  30.  
  31. </div>
  32.  
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two divs with class of "next":
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".first"
  16. ).
  17. nextAll
  18. ();
  19.  
  20.  
  21. //This code returns the one div with class "red" and innerHTML "Red Two".
  22.  
  23. dojo
  24. .
  25. query
  26. (
  27. ".first"
  28. ).
  29. nextAll
  30. (
  31. ".red"
  32. );
  33.  

prevAll

Returns all sibling elements that come before the nodes in this dojo.NodeList. Optionally takes a query to filter the prevIoUs elements.

The returned nodes will be in reverse DOM order -- the first node in the list will be the node closest to the original node/NodeList.

.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:

  1. <div
  2. class=
  3. "container"
  4. >
  5.  
  6. <div
  7. class=
  8. "red prev"
  9. >
  10. Red One</div>
  11.  
  12. Some Text
  13. <div
  14. class=
  15. "blue prev"
  16. >
  17. Blue One</div>
  18.  
  19. <div
  20. class=
  21. "red second"
  22. >
  23. Red Two</div>
  24.  
  25. <div
  26. class=
  27. "blue last"
  28. >
  29. Blue Two</div>
  30.  
  31. </div>
  32.  
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two divs with class of "prev":
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".first"
  16. ).
  17. prevAll
  18. ();
  19.  
  20.  
  21. //This code returns the one div with class "red prev" and innerHTML "Red One":
  22.  
  23. dojo
  24. .
  25. query
  26. (
  27. ".first"
  28. ).
  29. prevAll
  30. (
  31. ".red"
  32. );
  33.  

andSelf

Adds the nodes from the prevIoUs dojo.NodeList to the current dojo.NodeList.

.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:

  1. <div
  2. class=
  3. "container"
  4. >
  5.  
  6. <div
  7. class=
  8. "red prev"
  9. >
  10. Red One</div>
  11.  
  12. Some Text
  13. <div
  14. class=
  15. "blue prev"
  16. >
  17. Blue One</div>
  18.  
  19. <div
  20. class=
  21. "red second"
  22. >
  23. Red Two</div>
  24.  
  25. <div
  26. class=
  27. "blue"
  28. >
  29. Blue Two</div>
  30.  
  31. </div>
  32.  
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two divs with class of "prev",as well as the div with class "second":
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".second"
  16. ).
  17. prevAll
  18. ().
  19. andSelf
  20. ();
  21.  

first

Returns the first node in this dojo.NodeList as a dojo.NodeList.

This method is provided due to a difference in the Acme query engine used by default in Dojo. The Acme engine does not support ":first" queries,since it is not part of the CSS3 spec. This method can be used to give the same effect. For instance,instead of doing dojo.query("div:first"),you can do dojo.query("div").first().

Example

Assume a DOM created by this markup:

  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the div with class "blue" and "first" in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".blue"
  16. ).
  17. first
  18. ();
  19.  

last

Returns the last node in this dojo.NodeList as a dojo.NodeList.

This method is provided due to a difference in the Acme query engine used by default in Dojo. The Acme engine does not support ":last" queries,instead of doing dojo.query("div:last"),you can do dojo.query("div").last().

Example

Assume a DOM created by this markup:

  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the last div with class "blue" in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".blue"
  16. ).
  17. last
  18. ();
  19.  

even

Returns the even nodes in this dojo.NodeList as a dojo.NodeList.

This method is provided due to a difference in the Acme query engine used by default in Dojo. The Acme engine does not support ":even" queries,instead of doing dojo.query("div:even"),you can do dojo.query("div").even().

Example

Assume a DOM created by this markup:

  1. <div
  2. class=
  3. "container"
  4. >
  5.  
  6. <div
  7. class=
  8. "interior red"
  9. >
  10. Red One</div>
  11.  
  12. <div
  13. class=
  14. "interior blue"
  15. >
  16. Blue One</div>
  17.  
  18. <div
  19. class=
  20. "interior red"
  21. >
  22. Red Two</div>
  23.  
  24. <div
  25. class=
  26. "interior blue"
  27. >
  28. Blue Two</div>
  29.  
  30. </div>
  31.  
  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two divs with class "blue" in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".interior"
  16. ).
  17. even
  18. ();
  19.  

odd

Returns the odd nodes in this dojo.NodeList as a dojo.NodeList.

This method is provided due to a difference in the Acme query engine used by default in Dojo. The Acme engine does not support ":odd" queries,instead of doing dojo.query("div:odd"),you can do dojo.query("div").odd().

Example

Assume a DOM created by this markup:

  1. dojo
  2. .
  3. require
  4. (
  5. "dojo.NodeList-traverse"
  6. );
  7.  
  8.  
  9. //This code returns the two divs with class "red" in a dojo.NodeList:
  10.  
  11. dojo
  12. .
  13. query
  14. (
  15. ".interior"
  16. ).
  17. odd
  18. ();
  19.  

猜你在找的Dojo相关文章