html before table,Use greasemonkey to add HTML before table

问题

I am using greasemonkey to edit a page. I need to add my own table between the two tables that are already on the page and then remove the second table. There is nothing really setting the two existing tables apart, so I am having trouble with the function to insertBefore.

Table 1

12

Table 2

12
34

I have found the below code helpful in removing table 2, but I need to add my own table before table 2 first:

// find second

var xpathResult = document.evaluate('(//table[@class="details"])[2]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

var node=xpathResult.singleNodeValue;

// now hide it :)

node.style.display='none';

回答1:

This is a good chance to introduce jQuery. jQuery will be dead useful for the other things your GM script will do, plus, it's robust and cross-browser capable (for reusing your code).

(1) Add this line to the Greasemonkey metadata section, after the // @include directive(s):

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js

(Note you may have to uninstall and then reinstall the script to get jQuery copied over.)

(2) Then you can use this code to add your table and delete the old one:

//--- Get the 2nd table with class "details".

var jSecondTable = $("table.details:eq(1)");

//--- Insert my table before it.

jSecondTable.before

(

'

+ '

'

+ '

'

+ '

'

+ '

'

+ '

'

+ '

'

+ '

'

+ '

'

+ '

'

);

//--- Delete the undesired table.

jSecondTable.remove ();

/*--- Alternately, just hide the undesired table.

jSecondTable.hide ();

*/

You can see a version of this code, in action, at jsFiddle.

Alternate method of adding your table -- Less straightforward but does not require all the quotes:

jSecondTable.before ( (<>

]]>>).toString ()

);

来源:https://stackoverflow.com/questions/4636856/use-greasemonkey-to-add-html-before-table

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值