0

在 Firefox 扩展中使用 Javascript,我打开了一个新选项卡。我不知道如何在此选项卡中编写指向 www.google.com 的链接和其他链接(整个列表),用户可以在其中单击链接并打开此页面。

谢谢您的帮助

到目前为止,我输入了:

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());

不幸的是,这不起作用:

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newdocument=newTabBrowser2.contentDocument.documentElement.textContent;
newdocument.write("<a href=\"http://www.google.com\">google</a><br>");
newdocument.write("<a href=\"http://www.yahoo.com\">yahoo</a><br>");

我试过这个:

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newTabBrowser2.contentDocument.documentElement.innerHTML += "<a 

href=\" http://www.google.com \">谷歌
";

但这仅在我使用调试器时才有效

知道为什么吗?

谢谢

4

3 回答 3

1

I don't believe you can use textContent to add HTML content to a document - you're possibly better off using the DOM to construct the HTML.

How about something like this (untested):

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newdocument=newTabBrowser2.contentDocument.documentElement;

var link=newdocument.createElement("a");
link.setAttribute("href", "http://www.google.com");
link.textContent="google";
newdocument.appendChild(link);

newdocument.appendChild(newdocument.createElement("br"));

link=newdocument.createElement("a");
link.setAttribute("href", "http://www.yahoo.com");
link.textContent="yahoo";
newdocument.appendChild(link);

newdocument.appendChild(newdocument.createElement("br"));

Alternatively, it may be possible to just write to the innerHtml of the document element.

于 2009-06-01T12:41:33.840 回答
1

你的问题不是很清楚你想要什么。也许是这样的:

newwindow=window.open();
newdocument=newwindow.document;
newdocument.write("<a href=\"http://www.google.com\">google</a><br>");
newdocument.write("<a href=\"http://www.yahoo.com\">yahoo</a><br>");
newdocument.close();

???

于 2009-06-01T12:11:37.457 回答
0

这看起来像你正在寻找的东西。

http://mesh.typepad.com/blog/2004/11/creating_a_new_.html

var myUrl = "http://mesh.typepad.com";
var tBrowser = document.getElementById("content");
var tab = tBrowser.addTab(myUrl);

这会在每次运行时创建一个新选项卡 - 您可以像这样更新预先存在的选项卡的 url:

var uri = "http://mesh.typepad.com";
tBrowser.getBrowserForTab(tab).loadURI(uri);

最后,您应该能够将焦点设置到新选项卡:

tBrowser.selectedTab = tab;
于 2009-06-01T11:51:33.567 回答