通过innerHTML插入Javascript函数时出现ReferenceError

发布于 2024-12-14 04:02:06 字数 1627 浏览 2 评论 0原文

我在调用通过innerHTML 属性插入

的Javascript 函数时遇到问题。我在 SO 中搜索了类似的线程,但无法找到该问题的解决方案。

在简化版本中,这是我所拥有的:

MainPage.php

// ... Basic Headers 
<div id = 'list'>
    {include file="list.tpl"}
</div>
// More stuff...
<script type="text/javascript">
    function callbackFunction(data) {
        $('list').innerHTML = data.updatedListHTML;
        updateCalculatedFields();
    }
    // Perform a Ajax POST Request with the callbackFunction()
</script>

List.tpl

{if $thereIsData}
    // Create a table, populate its content...
    <script type="text/javascript">
        function updateCalculatedFields() {
             // Change some of the values in the created table...
        }
    </script>
{/if}

使用上面的代码,我在 callbackFunction( data),同时尝试调用 updateCalculatedFields() 函数。我仔细检查了innerHTML 是否已正确更新;updateCalculatedFields() 函数(以及整个脚本标记)已添加到div 中。然而,Javascript 似乎无法找到注入的函数。

只需稍加改动,该功能就可以工作。如果将 list.tpl 更改为:

List.tpl(版本 2)

{if $thereIsData}
    // Create a table, populate its content...
{/if}
<script type="text/javascript">
    function updateCalculatedFields() {
         // Change some of the values in the created table...
    }
</script>

两种情况之间的不同之处在于,在第二种情况下,updateCalculatedFields() 函数从一开始就随页面一起加载。在第一个中,它稍后与innerHTML 一起添加。所以,我的结论是,Javascript 不会注册通过innerHTML 添加的函数。但我非常怀疑情况是否如此;因此,如果有人能够对这个问题做出解释,那就太好了。

I have a problem calling a Javascript function that was inserted into a <div> via the innerHTML property. I have searched through similar threads in SO, but I wasn't able to find a solution to this problem.

In a simplified version, here is what I have:

MainPage.php

// ... Basic Headers 
<div id = 'list'>
    {include file="list.tpl"}
</div>
// More stuff...
<script type="text/javascript">
    function callbackFunction(data) {
        $('list').innerHTML = data.updatedListHTML;
        updateCalculatedFields();
    }
    // Perform a Ajax POST Request with the callbackFunction()
</script>

List.tpl

{if $thereIsData}
    // Create a table, populate its content...
    <script type="text/javascript">
        function updateCalculatedFields() {
             // Change some of the values in the created table...
        }
    </script>
{/if}

With the code above, I get a ReferenceError in the callbackFunction(data) while trying to call the updateCalculatedFields() function. I have double-checked to see that the innerHTML has been properly updated;, the updateCalculatedFields() function (and the whole script tag) has been added to the div. However, Javascript cannot seem to find injected function.

With a small change, the functionality works. If the list.tpl were to be changed to:

List.tpl (version 2)

{if $thereIsData}
    // Create a table, populate its content...
{/if}
<script type="text/javascript">
    function updateCalculatedFields() {
         // Change some of the values in the created table...
    }
</script>

The different between the two scenarios is that in the second one, the updateCalculatedFields() function is loaded with the page right from the start. In the first one, it gets added later with the innerHTML. So, my conclusion is that Javascript will not register functions that get added via innerHTML. I highly doubt that this is the case though; so it would be great if someone could give an explanation on this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-12-21 04:02:06

使用innerHTML 插入时,不会评估JavaScript。您需要自己对其进行 eval() 或将新的外部脚本标记附加到页面。

JavaScript is not evaluated when inserted with innerHTML. You either need to eval() it yourself or append a new external script tag to the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文