1

我正在使用 Greasemonkey 和 JavaScript 为网页编写注释。我想将相同的内容添加<td>到多行,但代码仅适用于第一行。

这是我所拥有的:

var headings2 = document.evaluate('/html/body/table/tbody/tr/td[2]/font/center[2]/table[2]/tbody/tr/td/table/tbody/tr[position()>1]/td[2]',document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
var thisHeadings2 = headings2.iterateNext(); 
var elmAnnotation2 = document.createElement('td');
  elmAnnotation2.innerHTML = 
    '<font size="1" face="Verdana, Arial" color="#000080">' +
    '<b>Response</b>' +
    '</font>';
while(thisHeadings2){
  thisHeadings2.parentNode.insertBefore(elmAnnotation2.cloneNode(true), thisHeadings2);
  thisHeadings2 = headings2.iterateNext();
}
4

1 回答 1

0

感谢@dandavis。据此:http ://help.dottoro.com/ljtfvvpx.php ,修改文档会使迭代无效,但不会使快照集合无效。所以现在我正在使用快照并修复它。这是解决方案:

var headings2 = document.evaluate('/html/body/table/tbody/tr/td[2]/font/center[2]/table[2]/tbody/tr/td/table/tbody/tr[position()>1]/td[2]',document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
var elmAnnotation2 = document.createElement('td');
elmAnnotation2.innerHTML = 
    '<font size="1" face="Verdana, Arial" color="#000080">' +
    '<b>Response</b>' +
    '</font>';    
for(var i = 0; i < headings2.snapshotLength; i++){
    headings2.snapshotItem(i).parentNode.insertBefore(elmAnnotation2.cloneNode(true), headings2.snapshotItem(i));
}
于 2013-06-15T02:41:01.017 回答