2016-09-07 87 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>