gpt4 book ai didi

javascript - Safari 引用错误 : Can't find variable

转载 作者:行者123 更新时间:2023-11-29 09:59:31 24 4
gpt4 key购买 nike

我在自己的 DIV 中有以下内容

<script>
function newfunc()
{
alert("here");
}
</script>
<button type="button" onclick="newfunc()">Press me</button>

在 IE 和 FF 上它工作正常,但在 Safari(至少 Safari 4.0.5)上它说“ReferenceError 找不到变量”——现在内容被动态加载到 DIV 中,Safari 似乎看不到此 DIV 中的函数定义 - 但是,如果我将函数 newfunc() 放在主 HTML 页面上(即不在 DIV 中),则按下按钮会调用该函数。

就好像 Safari 没有看到已经添加到 DIV 中的 Javascript 函数。

如有任何帮助,我们将不胜感激。

亚当

最佳答案

动态添加到页面的 Javascript 需要进行 eval-d 才能正确初始化。

为了使函数定义能够正确工作,它们的格式需要略有不同。例如:

<script>
newfunc = function()
{
alert("here");
}
</script>
<button type="button" onclick="newfunc()">Press me</button>

将内容添加到 DIV 后,从 DIV 中提取所有标签元素并评估其内容。

例如,使用 PrototypeJS (http://www.prototypejs.org/api/):

$(divid).innerHTML.evalScripts();

其中 divid 是您刚刚填充内容的 div 的 ID。

关于javascript - Safari 引用错误 : Can't find variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4387755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com