2016-09-07 88 views
2

下面的html是Clicky.com主頁儀表板的精簡版本,我期待隱藏論壇div標籤總共有一個H4標籤,其中包含短語「Clicky論壇」。根據H4標記中包含的特定文本隱藏div

<div class="fl small nounderline nowrap"> 
    <h4 class="inline">Clicky Forums</h4> &nbsp; 
    <a class="no-ajax" href="/forums/">See more...</a> 
    <br>&nbsp; &nbsp; 
    <span class="">Sep 6</span>&nbsp; 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18929">Adding goal to form submission javascrip...</a> 
    <br>&nbsp; &nbsp; 
    <span class="">Sep 5</span>&nbsp; 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18928">no show visitor statistic</a> 
    <br>&nbsp; &nbsp; 
    <span class="">Sep 3</span>&nbsp; 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18924">Number of new visitors per url in a spec...</a> 
    <br> 
</div> 
+0

[求助與隱藏DIV標籤,基於文本內容,使用Greasemonkey](https://stackoverflow.com/a/6921491) – wOxxOm

回答

4

您可以使用jQuery查找所有<h4>標籤然後檢查每個人的.text()的比賽。如果找到,.hide()元素的.parent()容器。

$('h4').each(function() { 
 
    $el = $(this); 
 
    
 
    if ($el.text() === 'Clicky Forums') { 
 
    $el.parent().hide(); // or .remove() 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>Stuff before</p> 
 

 
<div class="fl small nounderline nowrap"> 
 
    <h4 class="inline">Clicky Forums</h4> &nbsp; 
 
    <a class="no-ajax" href="/forums/">See more...</a> 
 
    <br>&nbsp; &nbsp; 
 
    <span class="">Sep 6</span>&nbsp; 
 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18929">Adding goal to form submission javascrip...</a> 
 
    <br>&nbsp; &nbsp; 
 
    <span class="">Sep 5</span>&nbsp; 
 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18928">no show visitor statistic</a> 
 
    <br>&nbsp; &nbsp; 
 
    <span class="">Sep 3</span>&nbsp; 
 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18924">Number of new visitors per url in a spec...</a> 
 
    <br> 
 
</div> 
 

 
<p>Stuff after</p>

請注意,這樣只會隱藏論壇的元素,你也可以使用.remove()完全取出來的DOM。

另外請注意,這是一個相當脆弱的解決方案,如果該部分的標題都改變(單個字母或資本的差異將打破查詢)可以打破。我建議嘗試找到一些更具體的選擇器,如ID,以標識論壇容器。

+0

Mr.Soviut是我真正的冠軍! :)像魅力一樣工作,謝謝你:) – Eshwar

+1

這不是一個很好的解決問題的方法。我會概述爲什麼在回答 – Soviut

+0

當然,是的請! :) – Eshwar

3

這裏是一個簡單的使用jQuery

$('h4:contains("Clicky Forums")').parent().hide(); 
+0

它是'父母'不是父母:) – Eshwar

+1

對,現在糾正了。謝謝! –

+0

歡迎您:) – Eshwar

1

您可以簡單地使用:

$("div h4:contains('Clicky Forums')").parent().hide() 

的片段:

$(function() { 
 
    $("div h4:contains('Clicky Forums')").parent().hide(); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 

 
<div class="fl small nounderline nowrap"> 
 
    <h4 class="inline">Clicky Forums</h4> &nbsp; 
 
    <a class="no-ajax" href="/forums/">See more...</a> 
 
    <br>&nbsp; &nbsp; 
 
    <span class="">Sep 6</span>&nbsp; 
 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18929">Adding goal to form submission javascrip...</a> 
 
    <br>&nbsp; &nbsp; 
 
    <span class="">Sep 5</span>&nbsp; 
 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18928">no show visitor statistic</a> 
 
    <br>&nbsp; &nbsp; 
 
    <span class="">Sep 3</span>&nbsp; 
 
    <a class="no-ajax" href="http://clicky.com/forums/?id=18924">Number of new visitors per url in a spec...</a> 
 
    <br> 
 
</div>