执行<脚本>里面< D​​IV>通过AJAX检索 [英] Executing <script> inside <div> retrieved by AJAX

查看:188
本文介绍了执行<脚本>里面< D​​IV>通过AJAX检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个名为内容的div:

 < D​​IV ID =内容>< / DIV>
 

应该从一个PHP文件中填充数据,通过AJAX,包括<脚本> 标记。但是,不执行此标签内的脚本。

 < D​​IV ID =内容><! - 经过AJAX加载了放在这里的东西 - >
   <脚本类型=文/ JavaScript的>
     // code
   < / SCRIPT>
   <! - 更多的东西,它在这里工作 - >
< / DIV>
 

解决方案

JavaScript的插入为DOM文本将不会执行。但是,您可以使用动态脚本模式,以实现自己的目标。其基本思想是将要执行到外部文件,并创建一个脚本标记,当你得到你的Ajax响应脚本。然后,设置脚本标签的的src 属性,瞧,它加载并执行外部脚本。

这其他的StackOverflow职位也可能对你有帮助:可脚本插入的innerHTML ?

There's a div called "Content":

<div id="content"></div>

It should be filled with data from a PHP file, by AJAX, including a <script> tag. However, the script inside this tag is not being executed.

<div id="content"><!-- After AJAX loads the stuff that goes here -->
   <script type="text/javascript">
     //code
   </script>
   <!-- More stuff that DOES work here -->
</div>

解决方案

JavaScript inserted as DOM text will not execute. However, you can use the dynamic script pattern to accomplish your goal. The basic idea is to move the script that you want to execute into an external file and create a script tag when you get your Ajax response. You then set the src attribute of your script tag and voila, it loads and executes the external script.

This other StackOverflow post may also be helpful to you: Can scripts be inserted with innerHTML?.

这篇关于执行&LT;脚本&GT;里面&LT; D​​IV&GT;通过AJAX检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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