【问题标题】:Add links using javascript使用 javascript 添加链接
【发布时间】:2011-05-13 16:12:39
【问题描述】:

我正在尝试在 Firefox 中使用 javascript 和greasemonkey 在谷歌主页底部添加一个链接,但我无法使其工作:

// ==UserScript==
// @name testing greasemonkey
// @include http://*google.com/
// ==/UserScript==    

document.write('<a href="http://bing.com">Go to Bing</a> ');

谁能帮帮我?

【问题讨论】:

  • 您真的要在 GOOGLE 主页底部添加链接吗??
  • 我觉得这很有趣,同时对谷歌也很刻薄,哈哈
  • 无论如何,我发布了一个答案...:)

标签: javascript firefox greasemonkey


【解决方案1】:

document.write 可能为时已晚。尝试将元素添加到 DOM。

var oNewA = document.createElement("a");
oNewA.setAttribute('href', 'http://bing.com');
var oText = document.createTextNode("Go to Bing");
oNewA.appendChild(oText);
document.body.appendChild(oNewA);

【讨论】:

    【解决方案2】:

    使用 DOM

    var link = document.createElement("a");
    link.href="http://bing.com";
    link.innerHTML="Go to Bing"
    document.body.appendChild(link);
    

    为了更有用的用法,你可以这样做:

    var link = document.createElement("a");
    link.href="http://bing.com";
    link.target="_blank";
    link.onclick=function() {
      this.href="http://www.bing.com/search?q="+escape(document.getElementsByName("q")[0].value);
    }
    link.innerHTML="Do the same search in Bing"
    document.body.appendChild(link);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2016-09-25
      • 2011-01-03
      相关资源
      最近更新 更多