获取“appcontent”位置.主机
我不是专业的 JavaScript 用户,并且在使用 addEventListener
时遇到困难。
var appcontent = document.getElementById("appcontent");
appcontent.addEventListener("DOMContentLoaded", load, true);
function load(aEvent) {
var doc = aEvent.originalTarget;
alert(doc.location.host);
}
在附加组件中,此代码将向 appcontent
的 location.host
发出警报。我的问题是,我不需要事件侦听器,并且想像普通函数一样调用 load:
var appcontent = document.getElementById("appcontent");
load(appcontent);
function load(aEvent) {
var doc = aEvent.originalTarget;
alert(doc.location.host);
}
这就是我试图做的,但它不起作用。
I am not an expert JavaScript user and I am having difficulty with addEventListener
.
var appcontent = document.getElementById("appcontent");
appcontent.addEventListener("DOMContentLoaded", load, true);
function load(aEvent) {
var doc = aEvent.originalTarget;
alert(doc.location.host);
}
In an add-on this code will alert the location.host
of the appcontent
. My problem is that I don't need an event listener and want to call load like a normal function:
var appcontent = document.getElementById("appcontent");
load(appcontent);
function load(aEvent) {
var doc = aEvent.originalTarget;
alert(doc.location.host);
}
This is what I was trying to do but it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
load()
函数仍然需要一个事件,但您现在将实际元素传递给它。另外,您想知道当前所选选项卡的哪个位置?然后您可以使用全局content
变量,它指向当前选项卡的window
对象。因此,将load()
函数更改为类似以下内容应该可行:使用 Ctrl+Shift+J 打开 JavaScript 控制台并检查扩展程序的错误 - 它应该可以帮助您找到问题所在。
Your
load()
function still expects an event but you are now passing the actual element to it. Also, which location do you want to know, that of the currently selected tab? Then you can use the globalcontent
variable, it is pointing to thewindow
object of the current tab. So changing theload()
function into something like this should work:Use Ctrl+Shift+J to open JavaScript Console and to check the errors for your extension - it should help you find our what the issue is.