通过 AJAX 加载脚本标签 [英] Loading script tags via AJAX
本文介绍了通过 AJAX 加载脚本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 div 标签,它通过 ajax 调用填充了脚本,但脚本没有执行.
I have a div tag which is filled with script via an ajax call, but the script does not execute.
有没有办法让脚本执行?
Is there a way to cause the script to execute?
推荐答案
如果您使用 jQuery 的 .html
方法,它会解析出脚本标记并对其进行评估:
If you use jQuery's .html
method it parses out the script tag and evals it:
$("div").html('<script type="text/javascript">alert("This should work")</script>');
如果 jQuery 不是一个选项,您可以使用 (1) 正则表达式或 (2) 解析 DOM 树并找到脚本标签自己编写.(#2 是 jQuery 的做法)
If jQuery isn't an option you could write this yourself using either (1) a regular expression, or (2) parse out the DOM tree and find script tags. (#2 is how jQuery does it)
这篇关于通过 AJAX 加载脚本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文