我正试着用我自己的桌子替换一张桌子,用的是油猴。包含该表的页面有2个表,它们具有相同的类,但没有ID。
我只需要替换第二个表(用我自己的表),而不需要对第一个表做任何操作。没有什么真正使第二个表与第一个表不同的东西,所以我唯一能想到的就是尝试在第二个表周围添加一个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>
发布于 2011-01-09 05:21:24
您可以使用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';
https://stackoverflow.com/questions/4635696
复制相似问题