使用Greasemonkey帮助基于文本内容隐藏DIV标签 [英] Help with hiding DIV tags, based on text content, using Greasemonkey

查看:104
本文介绍了使用Greasemonkey帮助基于文本内容隐藏DIV标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种编写Greasemonkey脚本的方法,该脚本将采用以下代码段,并且仅显示由<div class="A"组成的代码块,其中同时包含"passtest"和"State1".

I am looking for a way to write a Greasemonkey script which will take the following snippet of code and only show the code blocks consisting of <div class="A" where both "passtest" and "State1" are present.

<div class="A">
    <div class="B">
        <a href="/link1">
        <img class="imgClass" src="http://link.com/img.img" title="imgTitle"/>
        </a>
    </div>
    <div class="C">
        <span class="sc1">passtest</span>
        <br/>
        <em class="ec1">City1, State1</em>
    </div>
</div>
<div class="A">
    <div class="B">
        <a href="/link1">
        <img class="imgClass" src="http://link.com/img.img" title="imgTitle"/>
        </a>
    </div>
    <div class="C">
        <span class="sc1">failtest </span>
        <br/>
        <em class="ec1">City1, State1 </em>
    </div>
</div>
<div class="A">
    <div class="B">
        <a href="/link1">
        <img class="imgClass" src="http://link.com/img.img" title="imgTitle"/>
        </a>
    </div>
    <div class="C">
        <span class="sc1">passtest </span>
        <br/>
        <em class="ec1">City2, State2 </em>
    </div>
</div>


我是从 Dem Into Greasemonkey 找到的:


I found this from Dive Into Greasemonkey:

var allDivs, thisDiv;
allDivs = document.evaluate("//div[@class='sponsoredlink']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < allDivs.snapshotLength; i++) {
    thisDiv = allDivs.snapshotItem(i); 
    // do something with thisDiv 
}

我正在看这段代码,作为我想要做的事情的起点.但是,我只是一个用户,而不是编码人员.

I am looking at this code as the starting point for what I want to do. But, I am just a user, not a coder.

我了解我需要的逻辑是:
对于确实包含文本"passtest"且不包含"state1"的class ="a"的每个div,都不会显示该div.

I understand the logic I need is:
For each div where class="a" which does contain the text "passtest" and also does not contain "state1" do not display that div.

推荐答案

下面是一个使用 jQuery 的脚本.困难的部分是选择选择器.

Here's a script that does that, using jQuery. The hard part is choosing the selectors.

// ==UserScript==
// @name     Show State1 passes only.
// @include  http://YOUR_SITE/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

$("div.A")  .hide ()
            .has ("span.sc1:contains('passtest')")
            .has ("em.ec1:contains('State1')")
            .show ();

请注意, :contains()区分大小写.

Note that :contains() is case-sensitive.

> 在jsFiddle上查看运行中的代码.

这篇关于使用Greasemonkey帮助基于文本内容隐藏DIV标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆