ホームページ ウェブフロントエンド jsチュートリアル jquery_jquery で特定の要素が繰り返しバインドされる問題の簡単な分析

jquery_jquery で特定の要素が繰り返しバインドされる問題の簡単な分析

May 16, 2016 pm 05:05 PM
jquery

ある夜、コードを書いているときに突然バグが発生しました。長い間考えた後、どこに問題があるのか​​わかりませんでした(実際には非常に単純な問題でしたが、私はまだ初心者でした)。だから知りませんでした)。その間の過程は言うに及ばず、紆余曲折を経て、倒れそうになったときにようやくその理由が分かりました。同じ jquery 要素が繰り返しバインドされる可能性があり、ネストされたバインディングが使用されるとエラーが発生しやすくなることがわかりました。たとえば、コード:

コード をコピーします。 コードは次のとおりです。

$( '.test').bind ('click',function(){
$('.last').bind('click',function(){
alert('nihao');
});
} );


;

最初のボタンをクリックしてから 2 番目のボタンをクリックしても問題ありません。ただし、ページが更新される前に最初のボタンが複数回 (n 回) クリックされ、この時点で 2 番目のボタンをクリックすると問題が発生し、(n) 個の警告ダイアログ ボックスが表示されます。

解決策: 繰り返しバインドされる要素のバインドを解除します。つまり、unbind()、など:
コードをコピー コードは次のとおりです:

$('.test').bind('click',function(){
$ ('.last').unbind('click').bind('click',function(){
alert('nihao');
}); >

In this way, no matter how many times you click the first button, when you click the second button, only an alert dialog box will pop up.

Here are two more related to bind(), one() and live().
The
one() method attaches one or more event handlers to the selected element and specifies the function to run when the event occurs. When using the one() method, the event handler function can only be run once per element. In layman's terms, it only works once.

As for live(), quote what others said (http://www.cnblogs.com/wujilong/articles/1866834.html):
Usually when using jQuery for AJAX operations, newly generated The element events will become invalid, and sometimes you have to rebind the events, but this is very troublesome. For example, the JS verification of the comment content will fail after the comments are paginated. Before jQuery1.3, there was a plug-in that would solve this problem http://jquery.com/. jQuery1.3 added a live() method. The following is the description in the manual:

New method in jQuery 1.3. Bind an event handler (such as click event) to all current and future matching elements. Custom events can also be bound.

Currently supports click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup.

Blur, focus, mouseenter, mouseleave, change, submit

are not supported yet

Different from bind(), live() can only bind one event at a time.

This method is very similar to traditional bind. The difference is that using live to bind events will bind events to all current and future elements on the page (using delegation). For example, if you use live to bind click events to all li on the page. Then when a li is added to this page in the future, the click event of this newly added li is still available. There is no need to re-bind events to this newly added element.

.live() is very similar to the popular liveQuery plugin, but has the following major differences:

•.live currently only supports a subset of all events. Please refer to the description above for the supported list.
•.live does not support the "eventless" style callback function provided by liveQuery. .live can only bind event handling functions.
•.live does not have "setup" and "cleanup" processes. Because all events are delegated rather than directly bound to elements.

To remove events bound with live, please use the die method

Usage example:

jquery:
$(“.myDiv”).live(“click”, function(){
alert(“clicked!”);
});

If you use javascript to dynamically create an element with class mydiv, a pop-up will still appear when you click on the element. Why does it happen after using live? This is because jquery uses the event bubbling mechanism to directly bind the event to the document, and then finds the source of the event through event.target. This is different from the jquery.livequery plug-in. jquery.livequery checks every 20 milliseconds and rebinds the event if there is a new one.

Of course there are advantages and disadvantages to using live:

The advantage is: You don’t have to define events repeatedly when updating elements.
The disadvantage is: Binding the event to the document will call it once for every element on the page. Improper use will seriously affect performance. And it does not support blur, focus, mouseenter, mouseleave, change, submit.

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

jQueryのリファレンスメソッドを詳しく解説:クイックスタートガイド jQueryのリファレンスメソッドを詳しく解説:クイックスタートガイド Feb 27, 2024 pm 06:45 PM

jQuery 参照方法の詳細説明: クイック スタート ガイド jQuery は、Web サイト開発で広く使用されている人気のある JavaScript ライブラリであり、JavaScript プログラミングを簡素化し、開発者に豊富な機能を提供します。この記事では、jQuery の参照方法を詳しく紹介し、読者がすぐに使い始めるのに役立つ具体的なコード例を示します。 jQuery の導入 まず、HTML ファイルに jQuery ライブラリを導入する必要があります。 CDN リンクを通じて導入することも、ダウンロードすることもできます

jQueryでPUTリクエストメソッドを使用するにはどうすればよいですか? jQueryでPUTリクエストメソッドを使用するにはどうすればよいですか? Feb 28, 2024 pm 03:12 PM

jQueryでPUTリクエストメソッドを使用するにはどうすればよいですか? jQuery で PUT リクエストを送信する方法は、他のタイプのリクエストを送信する方法と似ていますが、いくつかの詳細とパラメータ設定に注意する必要があります。 PUT リクエストは通常​​、データベース内のデータの更新やサーバー上のファイルの更新など、リソースを更新するために使用されます。以下は、jQuery の PUT リクエスト メソッドを使用した具体的なコード例です。まず、jQuery ライブラリ ファイルが含まれていることを確認してから、$.ajax({u

jQueryで要素の高さ属性を削除するにはどうすればよいですか? jQueryで要素の高さ属性を削除するにはどうすればよいですか? Feb 28, 2024 am 08:39 AM

jQueryで要素の高さ属性を削除するにはどうすればよいですか?フロントエンド開発では、要素の高さ属性を操作する必要が生じることがよくあります。要素の高さを動的に変更する必要がある場合や、要素の高さ属性を削除する必要がある場合があります。この記事では、jQuery を使用して要素の高さ属性を削除する方法と、具体的なコード例を紹介します。 jQuery を使用して高さ属性を操作する前に、まず CSS の高さ属性を理解する必要があります。 height 属性は要素の高さを設定するために使用されます

jQuery のヒント: ページ上のすべての a タグのテキストをすばやく変更する jQuery のヒント: ページ上のすべての a タグのテキストをすばやく変更する Feb 28, 2024 pm 09:06 PM

タイトル: jQuery ヒント: ページ上のすべての a タグのテキストをすばやく変更する Web 開発では、ページ上の要素を変更したり操作したりする必要がよくあります。 jQuery を使用する場合、ページ内のすべての a タグのテキスト コンテンツを一度に変更する必要がある場合があります。これにより、時間と労力を節約できます。以下では、jQuery を使用してページ上のすべての a タグのテキストをすばやく変更する方法と、具体的なコード例を紹介します。まず、jQuery ライブラリ ファイルを導入し、次のコードがページに導入されていることを確認する必要があります: &lt

徹底した分析: jQuery の長所と短所 徹底した分析: jQuery の長所と短所 Feb 27, 2024 pm 05:18 PM

jQuery は、フロントエンド開発で広く使用されている高速、小型、機能豊富な JavaScript ライブラリです。 2006 年のリリース以来、jQuery は多くの開発者にとって最適なツールの 1 つとなっていますが、実際のアプリケーションでは、いくつかの利点と欠点もあります。この記事では、jQuery の長所と短所を詳しく分析し、具体的なコード例で説明します。利点: 1. 簡潔な構文 jQuery の構文設計は簡潔かつ明確であるため、コードの読みやすさと記述効率が大幅に向上します。例えば、

jQuery を使用してすべての a タグのテキスト コンテンツを変更する jQuery を使用してすべての a タグのテキスト コンテンツを変更する Feb 28, 2024 pm 05:42 PM

タイトル: jQuery を使用して、すべての a タグのテキスト コンテンツを変更します。 jQuery は、DOM 操作を処理するために広く使用されている人気のある JavaScript ライブラリです。 Web 開発では、ページ上のリンク タグ (タグ) のテキスト コンテンツを変更する必要が生じることがよくあります。この記事では、この目標を達成するために jQuery を使用する方法を説明し、具体的なコード例を示します。まず、jQuery ライブラリをページに導入する必要があります。 HTML ファイルに次のコードを追加します。

jQuery 要素に特定の属性があるかどうかを確認するにはどうすればよいですか? jQuery 要素に特定の属性があるかどうかを確認するにはどうすればよいですか? Feb 29, 2024 am 09:03 AM

jQuery 要素に特定の属性があるかどうかを確認するにはどうすればよいですか? jQuery を使用して DOM 要素を操作する場合、要素に特定の属性があるかどうかを判断する必要がある状況がよく発生します。この場合、jQuery が提供するメソッドを使用してこの関数を簡単に実装できます。以下では、jQuery 要素が特定の属性を持つかどうかを判断するために一般的に使用される 2 つの方法を紹介し、具体的なコード例を添付します。方法 1: attr() メソッドと typeof 演算子 // を使用して、要素に特定の属性があるかどうかを判断します

jQuery における eq の役割と応用シナリオを理解する jQuery における eq の役割と応用シナリオを理解する Feb 28, 2024 pm 01:15 PM

jQuery は、Web ページでの DOM 操作やイベント処理を処理するために広く使用されている人気のある JavaScript ライブラリです。 jQueryではeq()メソッドを利用して指定したインデックス位置の要素を選択しますが、具体的な使い方と応用シーンは以下の通りです。 jQuery では、 eq() メソッドは、指定されたインデックス位置にある要素を選択します。インデックス位置は 0 からカウントされます。つまり、最初の要素のインデックスは 0、2 番目の要素のインデックスは 1 などとなります。 eq() メソッドの構文は次のとおりです。 $("s

See all articles