2017-12-09 38 views
0

あなたは、ほとんどのページで動作しますuserscriptで「nestedmost」のdiv「から」の内容のみを設定するにはどうすればよいこのdivのgrand-grand -...-子供のコンテンツのみを ''に設定しますか?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>   
    <head>   
    <meta http-equiv="content-type" content="text/html; charset=utf-8">   
    <title>   
    </title>   
    </head>   
    <body>   
    <div>   
     <div>   
     How are you?   
     </div>   
     <div>    
     <div>    
      <div>    
      <p>     
       Hello    
      </p>    
      </div>    
     </div>   
     </div>   
    </div>   
    <div>   
    </div>   
    </body>  
</html> 

ようなHTMLがありますか?この文脈では、How are youが残っていますが、<p>Hello</p>は削除されています。

コンテキスト:フォーラムの投稿(つまり最小のユニット)や特定のキーワードを含む記事を自動的にフィルタリングすることですが、キーワードがネストされているためページ全体ではありません。

答えて

1

それはあなたが探しているものを私に完全には明らかではないのですが、ここでは文書本体ツリーで最も深いdivを見つける機能の関数です:あなたはの内容を削除するために使用することができ

function find_deepest_div() { 
    var deepest_div; 
    var deepest_div_depth = -1; 
    function search(current, depth) { 
     if (current.tagName === "DIV" && depth > deepest_div_depth) { 
      deepest_div = current; 
      deepest_div_depth = depth; 
     } 
     for (var i = 0, len = current.children.length; i < len; i++) 
      search(current.children[i], depth + 1); 
    } 
    search(document.body, 0); 
    return deepest_div; 
} 

次のような最も深いdiv:

var deepest_div = find_deepest_div(); 
if (deepest_div) deepest_div.innerHTML = ""; 
関連する問題