document.getElementById()在使用mozrepl(但不在firebug中)时返回null [英] document.getElementById() returns null when using mozrepl (but not in firebug)

查看:204
本文介绍了document.getElementById()在使用mozrepl(但不在firebug中)时返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用 mozrepl Firefox扩展程序为我提供Javascript REPL来自emacs。



我想我已经正确设置了。我可以通过emacs与REPL进行交互,并可以像教程页面中所描述的那样非常方便地浏览文档。问题出现在我尝试做一些非常简单的事情时,比如获取一个canvas元素的上下文:

pre $ code repl> document.getElementById(mycanvas)。getContext(2d)
!!! TypeError:document.getElementById(mycanvas)为空

详细信息:

message:document.getElementById(mycanvas)为空
fileName:chrome: //mozrepl/content/repl.js - > file:/// C:/Users/teamonkey/AppData/Roaming/Mozilla/Firefox/Profiles/chfdenuz.default/mozrepl.tmp.js
lineNumber:1
stack:
@chrome ://mozrepl/content/repl.js - > file:/// C:/Users/teamonkey/AppData/Roaming/Mozilla/Firefox/Profiles/chfdenuz.default/mozrepl.tmp.js:1
name:TypeError

这不仅仅是特定的实例:任何调用getElementById的函数都会返回null。



如果我启动了萤火虫,我可以输入相同的东西,它会返回一个有效的上下文,但我真的想让REPL在emacs中工作。我不认为这是一个错误,但我可能没有正确配置mozrepl。任何人都可以帮忙?



Mozrepl 1.0,Firefox 3.6

解决方案

你开始,你是在浏览器窗口本身的上下文中,而不是任何特定的文档。你可以访问铬元素(菜单,工具栏,选项卡等)。当前文档对象是指浏览器窗口。



要在活动选项卡中将文档切换到文档, p>

  repl.enter(内容)

现在文档对象是在活动选项卡中加载的文档,因此您可以访问它的DOM树并且可以对其进行操作。


I'm trying to use the mozrepl Firefox extension to give me a Javascript REPL from within emacs.

I think I've got it set up correctly. I can interact with the REPL from emacs and can explore the document pretty much as described in the tutorial pages. The problem comes when I try to do something really simple, like get a context to a canvas element:

repl> document.getElementById("mycanvas").getContext("2d")
!!! TypeError: document.getElementById("mycanvas") is null

Details:

  message: document.getElementById("mycanvas") is null
  fileName: chrome://mozrepl/content/repl.js -> file:///C:/Users/teamonkey/AppData/Roaming/Mozilla/Firefox/Profiles/chfdenuz.default/mozrepl.tmp.js
  lineNumber: 1
  stack:
    @chrome://mozrepl/content/repl.js -> file:///C:/Users/teamonkey/AppData/Roaming/Mozilla/Firefox/Profiles/chfdenuz.default/mozrepl.tmp.js:1
  name: TypeError

It's not just that particular instance: any call to getElementById will just return null.

If I start up firebug I can enter the same thing and it will return a valid context, but I'd really like to get the REPL working in emacs. I don't think this is a bug but I've probably not configured mozrepl correctly. Can anyone help?

Mozrepl 1.0, Firefox 3.6

解决方案

When you start, you are in the context of browser window itself, not any particular document. You have access to chrome elements (menus, toolbars, tabs, etc). The document object currently refers to browser window.

To switch context to the document in active tab use:

repl.enter(content)

Now document object is the document loaded in active tab, so you have access to its DOM tree nad can manipulate it.

这篇关于document.getElementById()在使用mozrepl(但不在firebug中)时返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆