【发布时间】:2011-04-12 13:02:53
【问题描述】:
我不确定这是 Prototype 特有的问题,但由于我在不使用 Prototype 时没有遇到问题,所以我猜是的。
我正在使用 Ajax.Updater 将一些外部 HTML 附加到我的 DOM 树中。在外部文件中有一些脚本元素。由于我已将 evalScripts 选项设置为 true,因此它们都会被评估。但是当我稍后尝试访问已在脚本元素中设置的对象时,它们不再存在。例如:
<script type="text/javascript">
var test = true;
console.log(test); // Works fine, obviously.
</script>
<input type="text" onkeydown="console.log(test)"> <!-- Throws an ReferenceError exception (test is not defined) when the event is fired. -->
如果我使用 Ajax.Updater 请求此脚本元素将按预期运行,但在评估之后测试变量似乎被删除。有谁知道怎么回事?
【问题讨论】:
-
试试
console.log(window.test)或者更好window.test = true