jQuery .load()不加载脚本 [英] jQuery .load() doesn't load script
问题描述
我在load_to.html页面中具有jQuery .load()
功能
I have jQuery .load()
function like in load_to.html page
$('#targetID').load('/load_from.html #bodyPart, script')
但是,这似乎并未从load_from.html页面加载javascript.有什么办法,我可以加载javascript.
However, this doesn't seems to be loading javascript from load_from.html page. Is there any way, I can load javascript.
推荐答案
来自jQuery的
jQuery使用浏览器的.innerHTML 属性来解析检索到的 文档并将其插入 当前文档.在此过程中, 浏览器经常从中过滤元素 文档,例如
<html>
,<title>
, 或<head>
元素.
jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as
<html>
,<title>
, or<head>
elements.
要加载脚本,您应该自己在文档的<head>
中创建<script>
元素:
To load scripts, you should create <script>
elements yourself in the document's <head>
:
$('<script>', {src: 'js_file.js'}).appendTo('head');
也许您可以请求使用ajax从服务器加载脚本列表:
Perhaps you can request a list of scripts to load from the server with ajax:
$.post('scripts_to_load.json', function (data) {
for (var i = 0; i < data.scripts.length; i++) {
$('<script>', {src: data.scripts[i]}).appendTo('head');
}
});
这篇关于jQuery .load()不加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!