我也遇到了这个错误,并确定 fastclick 是罪魁祸首。我原本打算接受 Devin Smith 的回答,但 epegzz 关于 MutationEvents 被弃用的警告让我想到了MutationObservers,因为我还没有看到涉及它们的修复,所以我想我会分享我的解决方案。
var observer_config = { attributes: false, childList: true, subTree: false, characterData: false }
var observer = new MutationObserver( function(mutations) {
var self = this;
mutations.forEach(function(mutation){
// look for the container being added to the DOM
var pac_container_added = $(mutation.addedNodes).hasClass('pac-container');
// if it is, begin observing it
if (pac_container_added){
var pac_container = mutation.addedNodes[0];
self.observe(pac_container, observer_config);
}
// look for pac-items being added (as children of pac_container)
// This will not resolve if the observer on pac-container has not been created
var pac_item_added = $(mutation.addedNodes).hasClass('pac-item');
// when pac items are added, add the needsclick class
if (pac_item_added) {
$('.pac-item, .pac-item span').addClass('needsclick')
}
});
});
observer.observe(document.body, observer_config);
它比我希望的要复杂,因为我们不能只observer.observe('pac_container')
在顶层添加,因为它是异步添加的。幸运的是,该问题的解决方案也是 MutationObservers。
pac_container
我们在创建时添加另一个观察者。这样,它会检测到pac-items
正在添加的内容,当它们存在时,我们添加needsclick
类。
This is my first time using MutationObservers, so feedback/improvements would be appreciated. As you can see, I used both jquery, but it should be pretty easy to pull it out.