我正在尝试使用Firefox中的javascript和greasemonkey在谷歌主页底部添加一个链接,但我无法让它工作:
// ==UserScript==
// @name testing greasemonkey
// @include http://*google.com/
// ==/UserScript==
document.write('<a href="http://bing.com">Go to Bing</a> ');
任何人都可以帮助我吗?
答案 0 :(得分:4)
对于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);
答案 1 :(得分:1)
使用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);