我在Drupal语境中问这个问题,但答案可能是通用的,而不是特别依赖Drupal。
我有一些HTML元素可以通过jQuery / ajax进行更新/替换。在第一页加载时,元素由jQuery正确处理。但是,jQuery忽略了在ajax调用之后返回的数据中包含的链接。
// This standard function ensures that elements are properly processed after first page load:
Drupal.behaviors.events = function(context) {
// This works as intended. Clicking on links works as intended:
$('.event_browse_location .parent_locations a:not(.events-processed)', context).addClass('events-processed')
.bind('click', function() {
$.get(Drupal.settings.basePath + '/events/location/' + parseInt(this.id, 10), null, browseLocation);
return false;
});
}
var browseLocation = function(response) {
var result = Drupal.parseJson(response);
// New elements are properly added to the DOM here:
$('.event_browse_location').html(result.data);
// However, this does not seem to have any effect.
// The a elements in the new elements do not work as intended.
// jQuery does not process them at all.
$('.event_browse_location .parent_locations a:not(.events-processed)').addClass('events-processed')
.bind('click', function() {
$.get(Drupal.settings.basePath + '/events/location/' + parseInt(this.id, 10), null, browseLocation);
return false;
});
}
答案 0 :(得分:0)
在这种情况下,有两个愚蠢的错误。
第一个是guest271714指出的:我发布的原始代码中有一个未声明的变量。我已相应修改了这个问题。
其次,我的jQuery选择器是错误的,并且排除了要包含的元素。修复选择器修复了问题。