More about DOM

上次回来以后一直对DOM的manipulation念念不忘。每次遇到有关的题就看了又看。

Define a walk_the_DOM function that visits every node of the tree in HTML source order, starting from some given node.
  1. 当然是用recursion来做。Each recursive call is given a smaller piece of the tree to work on.
  2. It invokes a function, passing it each node in turn.
  3. walk_the_DOM calls itself to process each of the child nodes.
  4. Code:
Screen Shot 2016-03-22 at 11.46.39 PM.png
Define a getElementsByAttribute function. It takes an attribute name string and an optional matching value.
  1. 利用上面的walk_the_DOM. 传入的是DOM中<body>所在的node,以及一个检查node的attribute是不是和要求的att的value相符合的匿名函数。
  2. 结果存在一个数组里。
  3. Code:Screen Shot 2016-03-22 at 11.46.54 PM.png

你看其实又用到了nodeType的概念所以没文化真可怕啊!

1 thought on “More about DOM”

Leave a reply to Manipulating the DOM | My Blueberry Nights Cancel reply