現在、動的に生成された要素にコールバックを割り当てるオブジェクト指向モジュールを作成しています。
function Instant(containerID) {
this.var1 = 0;
this.var2 = 0;
this.containerID = containerID;
// and more variables...
};
そして、これは動的に生成されるcontainerID
a の id です。次のようなファイルを読み取る Ajax Request を介してこれを設定します。DIV
DIV
<!-- content.html -->
<div class="general_container">
<div class="top_container">
<!-- plenty of divs, spans etc -->
</div>
<div class="tweet_section">
<!-- plenty of divs, spans etc -->
</div>
</div>
ここで重要な部分は、次のようにすべてのコールバックを割り当てることです。
Instant.prototype.addCallbacks = function() {
$(this.containerID + " bar").click(function() {
$(this.containerID + " bar").foo();
});
$(this.containerID + " bar").click(function() {
$(this.containerID + " bar").foo();
});
$(this.containerID+ " bar").click(function(e) {
$(this.containerID + "bar, " + this.containerID+ " bar").foo();
});
});
ご覧のとおり、this.containerID
各セレクターの前にイベントを割り当てる必要があります。(したがって、1 つの要素のみを選択していることを確認します) s がたくさんあるので、コードは雑然としていますthis.containerID
。コードを簡単にするためのよりスマートな方法があるかどうかはわかりません。どんな助けでも大歓迎です。
サンプルJSFiddleを次に示します。
これは私の実際のモジュールではないことに注意してください。明確にするために作成しただけです!