[英]Load HTML/Javascript dynamically
我有一个index.html页面,我想使用jQuery动态地将另一个页面的内容加载到index.html。 我使用$ .load()函数来执行此操作:
$('#dynamic_content').load('another.html #toLoad')
它工作正常,但我还需要加载使用another.html的javascript文件,所以我这样做:
$('#dynamic_content').load('another.html #toLoad');
$.getScript('js/another.js');
问题是'another.js'的js代码有时不会'应用'到html页面(可能它比html页面更早加载)another.js的内容:
$(document).ready(function {} {
$('#cancelButton').click(function() {
//do something
});
});
使用成功回调:
$('#dynamic_content').load('another.html #toLoad', function() {
$.getScript('js/another.js');
});
这样,如果another.js
操纵来自another.html
的动态加载内容,则可以保证此内容已经注入到当前DOM树中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.