Greasemonkey:在两个元素之间寻找一个字符串 [英] Greasemonkey: look for a string between two elements

查看:97
本文介绍了Greasemonkey:在两个元素之间寻找一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Greasemonkey脚本,该脚本应该做一件简单的事情:如果<h2>元素(页面上始终只有一个)和第一次出现的<h3>元素(可以是几个或根本没有),然后...(我已经完成了这一部分).

I'm writing a Greasemonkey script, which should do a simple thing: if there's a certain string between the <h2> element (always only one on the page) and the first occurrence of an <h3> element (can be several of them or none at all), then ... (I've already done this part).

感谢您有条件的帮助.

推荐答案

现在我刚好熟悉Mikhail正在开发的代码.

now I just happen to be familiar with the codes Mikhail are working on.

基本结构是

<div>
<div>
<h2>Something</h2>
<td>We want to search for the string here</td>
<td>We want to search for the string here</td>
</div>

<div>
<h3>Something else</h3>
<td>May contain the same string, but we are only interested if it contains in previous div .</td>
<td>May contain the same string, but we are only interested if it contains in previous div .</td>
</div>
</div>

该字符串不是h2的子级,因此我认为getElementsByTagName不起作用.不幸的是,实际上有数百个具有相同类ID的div层.在这种特殊情况下,标题是代码中唯一的唯一细节.因此,我认为最好的方法是首先找到h2,转到其父级并将文本存储为字符串.然后在文本中搜索字符串.像这样的东西...

The string is not a child of h2, so I don't think getElementsByTagName would work. Unfortunately there are literally hundreds of div layer with same class id. In this particular case heading is the only unique details in the code. So in my opinion the best way is to find h2 first, go to its parent and store text as string. Then search for the string in the text. Soemthing like this...

<script>
var searcharea = jQuery('h2').parent('div').text();
var searchstring = "superstring";
if( searcharea.indexOf( searchstring ) != -1 )
    alert("exchange alert to your own things");
</script>

对于h3,它可能存在或可能不存在,但是由于它不是同级,所以实际上并不重要. :)谢谢大家花时间回答我们的问题.

As for h3, it may or may not exist, but since its not a sibling, it doesn't really matter. :) Thank you all for taking your time to answer our question.

这篇关于Greasemonkey:在两个元素之间寻找一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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