0
正如我在jQuery中发现的Ajax一样,加载的元素不是jQuery对象。重新扫描文档以使用Ajax jQuery对象加载元素的最佳方式是什么?加载元素jQuery对象
在下面的代码jq('.storyBlock button').click(function()
和其他函数不会触发加载的Ajax html。如何更好地改变这些代码,以便他们能够开火?
var jq = jQuery.noConflict();
jq(document).ready(function() {
// Disable buttons after clicking
jq('.storyBlock button').click(function() {
jq(this).addClass('selected');
jq(this).parent().addClass('disabled');
jq(this).parent().find('button').attr('disabled', true);
});
// Event Handler
jq('.storyBlock button').click(function() {
// Find link
var getID = jq(this).data('link');
console.log(getID + '.html');
// Load file and insert after last story
jq.ajax(getID + '.html').done(function(html) {
jq('#stories > div:last-child').after(html);
if ((getID) != 'start') {
jq('html,body').animate({ scrollTop: jq('#' + getID).offset().top },'slow');
} else {
jq('.storyBlock').remove();
jq('#stories > div').load("start.html");
jq('html,body').animate({ scrollTop: jq('#start').offset().top },'slow');
}
});
});
});
[事件代表团(http://learn.jquery.com/使用event delegation活动/事件委派/)! –
@ArunPJohny是正确的。如果你动态地向DOM添加元素,你将需要使用事件代理 – heymega
@ArunPJohny我不认为这是有益的大声“事件代表团!!!”即使问题涉及所涉及的技术的基本概念,也会降低这个问题。 – Filburt