从已经加载的内容中加载内容
load content from already loaded content
我有一个菜单,我可以从中将来自不同 html 页面的内容加载到我的 div id="content";我想从加载的内容中加载内容;不工作
我尝试了各种方法,但完全迷路了,比如 .live(click,..)、empty()、remove() 等。
我一次又一次得到的是相当的内容,但在我的 div 中没有,即没有
css,在加载的内容中单击 link 将带我到另一个页面,而不是将该页面的内容加载到我当前的页面。
$(document).ready(function() {
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('fast',loadContent);
function loadContent() {
$('#content').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#content').show('normal');
}
return false;
});
});
我明白了:问题是事件委托,意思是:加载的内容不是由 dom 结构注册的。所以你必须将点击事件委托给祖先,
最初加载的是:
/* 点击 - 事件委托给#staticAncestor /
/ $(staticAncestors).on(eventName, dynamicChild, function() {}); */
这是一种本地解决方案;我不再找到它,但也有全球答案
研究 'event delegation'
我有一个菜单,我可以从中将来自不同 html 页面的内容加载到我的 div id="content";我想从加载的内容中加载内容;不工作
我尝试了各种方法,但完全迷路了,比如 .live(click,..)、empty()、remove() 等。 我一次又一次得到的是相当的内容,但在我的 div 中没有,即没有 css,在加载的内容中单击 link 将带我到另一个页面,而不是将该页面的内容加载到我当前的页面。
$(document).ready(function() {
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('fast',loadContent);
function loadContent() {
$('#content').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#content').show('normal');
}
return false;
});
});
我明白了:问题是事件委托,意思是:加载的内容不是由 dom 结构注册的。所以你必须将点击事件委托给祖先, 最初加载的是:
/* 点击 - 事件委托给#staticAncestor / / $(staticAncestors).on(eventName, dynamicChild, function() {}); */
这是一种本地解决方案;我不再找到它,但也有全球答案 研究 'event delegation'