遍歷DOM樹

function traverseDOM(element,callback){
    callback(element);
    element=element.firstChild;
    while(element){
        traverseDOM(element,callback);
        element=element.nextElementsibling;
    }
}
const subTree=document.getElementbyId('subTree');
traverseDOM(subTree,function(element){
  assert(element !==null, element.nodeName);
});

經過遞歸語法實現遍歷dom樹node

function* traverseDOM(element){
  yield element;
  element = element.firstChild;
  while(element){
    yield* traverseDOM(element);
    element = element.nextElement.sibling;
  }
}
const subTree= document.getElementbyId('subTree');
for(element of traverseDOM(subTree)){
  assert(element !==null,element.nodeName);
}

經過生成器實現遍歷dom樹dom

//本身測試的時候能夠把assert 函數改爲console.log()函數。函數

//但願天天都能進步一點點測試

相關文章
相關標籤/搜索