2011-11-17 85 views

回答

8

定位您真正關心的節點,然後使用間隔計時器檢查更改。

的關鍵是選擇感興趣的節點,例如給定:

<div id="content"><p class="author"> ... </p> 
... 
</div> 

也許你想要的「作者」的節點。
您將在計時器內使用querySelectorAll("#content p.author")進行掃描。

像這樣的事情代碼:

setInterval (MyNodeCheckFunc, 500); 

function MyNodeCheckFunc() 
{ 
    var newNodeList  = []; 
    this.oldNodeList = this.oldNodeList || []; 

    var targetNodes  = document.querySelectorAll ('VALID CSS-STYLE SELECTOR'); 
    if (targetNodes && targetNodes.length > 0) 
    { 
     /*--- Found target node(s). Go through each and act if they 
      are new. 
     */ 
     for (var J = targetNodes.length - 1; J >= 0; --J) 
     { 
      var targNode = targetNodes[J]; 

      newNodeList.push (targNode); 

      if (! targNode.weHaveProcessed) 
      { 
       targNode.weHaveProcessed = true; 

       //// DO STUFF FOR NEW NODE. 
      } 
     } 
    } 

    /*/// HERE, COMPARE newNodeList TO oldNodeList. 
     1) If an item is in oldNodeList, but not in newNodeList, 
      do any DELETED NODE action. 

     2) If an item is in a different order from oldNodeList 
      to newNodeList, do any MOVED NODE action. 
    */ 
    this.oldNodeList = newNodeList; 
} 
相關問題