【发布时间】:2014-03-21 23:35:48
【问题描述】:
我有一个主 html 页面,它使用 jQuery 将其他 html 页面加载到 div 中。像这样:
$('.controlPanelTab').click(function() {
$(this).addClass('active').siblings().removeClass('active');
var name = $(this).attr('name');
//create the html doc name + append it to the displayPanel Div
var docName = name + ".html";
$("#displayPanel").load(docName);
});
到目前为止,页面显示正确,引用了正确的样式表等。
但是,在与加载的页面(在 div 内)进行交互时会出现问题。例如,其中一个加载的页面有一个文本区域,专注于点击,但不允许输入文本。另一个页面使用 jQuery 动画显示屏幕上的一些统计信息。直接在 URL 栏中访问该页面会显示正确的动画统计信息,但是一旦使用上述方法加载到 div 中,就不会出现动画。
好像主页无法识别加载的页面,因为它设置得太晚了,也许一开始就一次性加载所有内容可以解决这个问题。但如果可能的话,我更愿意采用延迟加载的方法,并将这些“子”html 页面分开。
有没有办法刷新主页(DOM?),以便正确识别新元素(如果这确实是问题)?
编辑 有效的代码:
$(document).on('click', '.controlPanelTab', function() {
$(this).addClass('active').siblings().removeClass('active');
var name = $(this).attr('name');
//create the html doc name + append it to the displayPanel Div
var docName = name + ".html";
$("#displayPanel").load(docName);
//create external .js file name and call it up
docName = name + ".js";
$.getScript(docName, function( data, textStatus, jqxhr ) {
if (textStatus==="success") {
lightItUp(); //load method
}
});
});
【问题讨论】:
-
尝试事件委托 -> api.jquery.com/on
-
请发布您的外部 html 和动态加载的 html。
-
感谢大家的帮助。
标签: javascript jquery html lazy-loading