2017-12-09 42 views
0

은 당신이 당신이 대부분의 페이지에서 작동하는 userscript에서 ''에 "nestedmost"DIV의 내용을 설정하는 방법이div의 grand-grand -...- children의 콘텐츠 만 어떻게 설정합니까?

<!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>은 제거됩니다.

컨텍스트 : 목표는 자동으로 포럼 게시물 (즉, 가장 작은 단위) 또는 특정 키워드가 포함되어 있지만 전체 페이지가 아닌 기사가 키워드가 중첩되어 있기 때문에 필터링하는 것입니다.

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; 
} 

당신은의 내용을 삭제하는 데 사용할 수 있습니다 :

답변

1

그것은 여기 당신이 찾고있는 무엇을 나에게 완전히 명확하지 않다,하지만 문서 본문 트리에 div 가장 깊은 발견 기능을하는 기능입니다 가장 깊은 div는 이렇게 이렇게 :

var deepest_div = find_deepest_div(); 
if (deepest_div) deepest_div.innerHTML = "";