2011-01-08 47 views
2

我试图用我自己的表使用油脂猴替换表。具有表格的页面有两个具有相同类别且没有ID的表格。使用Greasemonkey删除表

我只需要替换第二个表(用我自己的表),而对第一个表不做任何操作。没有什么能够让第二张表在第一张表中独一无二,所以我唯一能想到的就是试图在第二张表上添加一个DIV,但无法弄清楚。

任何想法?下面是页码:

<h3>Table 1</h3> 
<table class="details" border="1" cellpadding="0" cellspacing="0"> 
<tbody><tr> 
<th>1</th> 
<td>2</td> 
</tr> 
</tbody></table> 

<h3>Table 2</h3> 
<table class="details" border="1"> 
<tbody><tr> 
<th>1</th> 
<td>2</td> 
</tr><tr> 
<th>3</th> 
<td>4</td> 
</tr> 
</tbody></table> 

回答

0

您可以使用xpath查找第二个表,并对其执行一些操作。 xpath表达式将是(//table[@class="details"])[2]。下面我添加了一个使用(//pre)[1]的例子,它会在这个页面上找到第一个代码块,我将把它作为一个例子来隐藏它。所以这隐藏了你的问题的页面代码。 (//pre)[2]将隐藏我的脚本。

另请参阅here了解如何使用xpath的教程。

// ==UserScript== 
// @name   so-test 
// @namespace  test 
// @include  http://stackoverflow.com/questions/4635696/use-greasemonkey-to-remove-table 
// ==/UserScript== 

// find first <pre> on this page 
var xpathResult = document.evaluate('(//pre)[1]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); 
var node=xpathResult.singleNodeValue; 

// now hide it :) 
node.style.display='none'; 
+1

另外,使用`document.getElementsByClassName( '细节')项(1)` – user123444555621 2011-01-08 21:28:26