【发布时间】:2018-06-18 19:58:47
【问题描述】:
我正在编写一个函数,使用带有 jQuery 的突变观察器来注册对 DOM 的更改,特别是当添加新节点时,我可以更改其内容:
$("SELeCTOR GOOD" ).click(function(){
var targetNode = $(this).find('.content').get(0);
var config = { attributes: true, childList: true, subtree: true, attributeOldValue: true };
// Callback function to execute when mutations are observed
var callback = function(mutationsList) {
for(var mutation of mutationsList) {
if (mutation.type == 'childList') {
var courses = $(targetNode).find('.courses').get(0);
$(courses).find('.coursebox.clearfix').each(function( index,item ) {
var attacherURL = $(item).find('.coursename a').attr('href');
var moreInfoURL = '<a class="btn btn-outline-primary pull-right" href="'+attacherURL+'">More info</a>';
var oldHTML = $(item).find('div.moreinfo').html();
var newHTML = moreInfoURL + oldHTML;
//this following line is supposed to replace the html, but it creates an infinite loop
$(item).find('div.moreinfo').html(newHTML);
//end
});
}
else if (mutation.type == 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
我也尝试过追加/前置,但一切都会创建相同的无限循环。像往常一样,非常感谢任何帮助。
问候
【问题讨论】:
标签: javascript jquery moodle