添加链接使用JavaScript
问题描述:
我尝试添加在使用JavaScript和的Greasemonkey在Firefox谷歌的主页底部的链接,但我不能让它工作:添加链接使用JavaScript
// ==UserScript==
// @name testing greasemonkey
// @include http://*google.com/
// ==/UserScript==
document.write('<a href="http://bing.com">Go to Bing</a> ');
谁能帮助我?
答
对于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);
答
使用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);
你真的想在Google主页的底部添加一个链接? – ascanio 2011-05-13 16:17:52
我发现这有点有趣,并意味着谷歌同时大声笑 – tradyblix 2011-05-13 16:18:36
无论如何,我张贴了一个答案... :) – ascanio 2011-05-13 16:19:54