通过AJAX加载脚本标记 [英] Loading script tags via AJAX
本文介绍了通过AJAX加载脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是通过Ajax调用充满了脚本div标签,但该脚本不执行。
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
方法,它分析出来的脚本标记和evals它:
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)定期EX pression,或(2)解析出DOM树,找到script标签。 (#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屋!
查看全文