2

我正在寻找将带有 greasmonkey 的图像插入 href 的最佳方法。图片应该在现在文本(??????)所在的位置。

   // ==UserScript==
   // @name           ...
   // @description    ...
   // @include        http://website/
   // @version        1.0
   // ==/UserScript==
    var links = document.getElementsByClassName( 'xyz' );
    for ( var i = 0; i < links.length; i++ ) {
    var link = links[i];
    var newlink = document.createElement( 'a' );
    newlink.href = link.href.replace( 'value=10', 'value=20' );

    newlink.textContent = '??????';

    var nextNode = link.nextSibling;
    link.parentNode.insertBefore( newlink, nextNode );
    var delim = document.createTextNode( ' - ' );
    link.parentNode.insertBefore( delim, newlink );
    }

谢谢大家的帮助。

4

2 回答 2

2
var links = document.getElementsByClassName( 'xyz' );
for ( var i = 0; i < links.length; i++ ) {
    var link = links[i];
    var newlink = document.createElement( 'a' );
    newlink.href = link.href.replace( 'value=10', 'value=20' );
    var the_image = document.createElement('img');
    the_image.src = '';//Place image source file path here
    newlink.appendChild(the_image);

    var nextNode = link.nextSibling;
    link.parentNode.insertBefore( newlink, nextNode );
    var delim = document.createTextNode( ' - ' );
    link.parentNode.insertBefore( delim, newlink );
}
于 2012-10-05T14:22:08.293 回答
2

插入以下行而不是带有 的行???

var myImg = document.createElement('img');
myImg.src= 'path/to/image';
newlink.appendChild(myImg);
于 2012-10-05T14:22:17.670 回答