我将如何更改以下代码(使 timeago 插件工作)以使用 on() 函数(或其他函数)以便可以实时使用?我似乎不太明白(我对 JQuery 很陌生)。
jQuery(document).ready(function() {
jQuery("a.timeago").timeago();
});
这将如何完成?
我将如何更改以下代码(使 timeago 插件工作)以使用 on() 函数(或其他函数)以便可以实时使用?我似乎不太明白(我对 JQuery 很陌生)。
jQuery(document).ready(function() {
jQuery("a.timeago").timeago();
});
这将如何完成?
您可以使用 MutationObserver 检查 DOM 何时被修改:https ://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
你选择一个节点来观察。对于您的项目,您应该观察a.timeago
将在其中创建元素的容器。<body>
很好,但会为浏览器带来更多工作。然后,只要 dom 发生变化,就会触发回调。抓住你关心的元素。
从文档修改:
// select the target node
var target = document.querySelector('#your-container');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(addedNode) {
// if the addedNode matches $('a.timeago')
// $(addedNode).timeago();
});
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing
observer.disconnect();