在href中插入图像的最佳方法

时间:2012-10-05 14:14:11

标签: javascript dom greasemonkey href

我正在寻找将带有greasemonkey的图像插入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 );
    }

谢谢大家的帮助。

2 个答案:

答案 0 :(得分: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 );
}

答案 1 :(得分:2)

插入以下行而不是???

var myImg = document.createElement('img');
myImg.src= 'path/to/image';
newlink.appendChild(myImg);