1

on() 関数 (またはその他) を使用してライブで使用できるようにするには、次のコード (timeago プラグインを機能させる) をどのように変更すればよいでしょうか? 私はそれを理解できないようです(私はJQueryにかなり慣れていません)。

jQuery(document).ready(function() {
  jQuery("a.timeago").timeago();
});

これはどのように行われますか?

4

1 に答える 1

0

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();
于 2013-09-15T04:23:36.953 に答える