Expand events tutorials to cover cases where bound elements are removed and re-added.

オープン
#624 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
ドキュメント
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
javascript
領域
documentation

調査の方向性

既存のイベントチュートリアルと .remove() のドキュメントから始め、動機となる例として委譲された .on('click', '.someChild', ...) の例を使用します。バインドされた要素が削除されて再追加されたときに何が起こるかを、.html() による削除も含めて説明するようチュートリアルを拡張し、.detach() がハンドラーを保持する場合を明確にします。

索引モデルが issue の本文から書いたものです。

説明

Originally filed in jquery/api.jquery.com#632.

There seems to be pretty regular confusion surrounding the notion that event bindings break if the element you bound to was removed from the document (via, say, .remove()) and then re-added (like when you create a new element with $()).

The specific issue is that people think that simply using a delegated listener is enough - e.g.:

$('.someDiv').on('click', '.someChild', function() {
    $('.someDiv').remove();
});

They then later re-add $('.someDiv') and expect this to work because the handler is technically bound to an element that exists at the time of binding - it just happened to be removed and re-added.

The .remove() docs helpfully explain that:

In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach() instead.

This is fine, except that a lot of people aren't going to think to go to docs for .remove() while reading about events. This also doesn't cover cases where somebody ran something like .html('<span>no cookie for you</span>') on a parent of the bound node, wiping it out.

主要言語
JavaScript
スター
914
フォーク
472
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

jquery/learn.jquery.com のほかの issue

jquery/learn.jquery.com の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。