我如何使用$ .ajax在加载了jquery的内容中包含javascript [英] How can I include javascript in content loaded with jquery using $.ajax

查看:91
本文介绍了我如何使用$ .ajax在加载了jquery的内容中包含javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery加载一些包含一些JavaScript的HTML.我试过同时使用load()和ajax(). HTML很好地插入了DOM,但是所有脚本标记似乎都被过滤掉了.如果我对返回的HTML发出alert()警报,则包括脚本,但是当我使用html()或append()时,脚本将丢失.

I want to load some HTML which include a bit of javascript, using jQuery. I've tried using both load() and ajax(). The HTML is inserted nicely into the DOM, but any script-tags seems to be filtered out. If I alert() the returned HTML, the scripts are included, but when i use html() or append(), the scripts are missing.

有什么想法吗?

推荐答案

您应使用 加载并执行远程Javascript:

You should use $.getScript to load and execute remote Javascript:

加载并执行本地 使用HTTP GET的JavaScript文件 请求.

Loads, and executes, a local JavaScript file using an HTTP GET request.

示例:

$.getScript("test.js", function(){
  alert("Script loaded and executed.");
  $('#myDiv').load('some.html');
});

这篇关于我如何使用$ .ajax在加载了jquery的内容中包含javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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