検索
ホームページウェブフロントエンドjsチュートリアルjquery_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 までご連絡ください。
jquery实现多少秒后隐藏图片jquery实现多少秒后隐藏图片Apr 20, 2022 pm 05:33 PM

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

axios与jquery的区别是什么axios与jquery的区别是什么Apr 20, 2022 pm 06:18 PM

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

jquery怎么修改min-height样式jquery怎么修改min-height样式Apr 20, 2022 pm 12:19 PM

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

jquery怎么在body中增加元素jquery怎么在body中增加元素Apr 22, 2022 am 11:13 AM

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

jquery怎么删除div内所有子元素jquery怎么删除div内所有子元素Apr 21, 2022 pm 07:08 PM

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

jquery中apply()方法怎么用jquery中apply()方法怎么用Apr 24, 2022 pm 05:35 PM

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

jquery on()有几个参数jquery on()有几个参数Apr 21, 2022 am 11:29 AM

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

jquery怎么去掉只读属性jquery怎么去掉只读属性Apr 20, 2022 pm 07:55 PM

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

See all articles

ホット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ヘンタイを無料で生成します。

ホットツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

SublimeText3 Mac版

SublimeText3 Mac版

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

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール