経験豊富なjQueryユーザーはイベントのバインディングに精通していますが、最適化するとコードの堅牢性と保守性が向上します。この記事では、より効果的なjQueryイベントのバインディングのための手法について説明します。
重要な改善:属性を使用すると、プレゼンテーションと相互作用ロジックが分離されます。 CSSクラスの変更はJavaScriptを破りません
<https:><https:>名前付き関数ハンドラーとしての関数:<li>匿名関数を指定された関数に置き換えると、読みやすさが向上し、再利用可能性が向上し、テストが簡素化されます。
正確な制御のための名前空間:<strong>jQueryの名前空間機能は、特にプラグインでのイベントの結合とバインディング、単純化のクリーンアップを粒状に制御することを提供します。
<https:>
<https:><li>拡張セレクター戦略:<strong><https:>
<https:>シンプルなナビゲーションメニューの切り替えを検討してください:<https:>
<p> <strong><https:>トグルNAVメニュー<https:>
<p>
<https:>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><code class="language-html">西フィラデルフィア<button data-hook="nav-menu-toggle"><https:>
<nav data-hook="nav-menu"><a href="//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712cab">キャブホイッスル<https:><https:>
<li><a href="//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712throne">玉座に座っている<https:><https:>
<https:>
<https:> <https:><https:>
CSSクラスに依存する代わりに<p>:<https:>
<pre class="brush:php;toolbar:false"><code class="language-javascript">$( '。vav-menu-toggle')。on( 'click'、function(){
$( 'nav')。toggle();
}); <https:><https:>
<p>より弾力性のあるコードのために<code>data-hook<https:>属性を使用してください:<https:>
<pre class="brush:php;toolbar:false"><code class="language-javascript">$( '[data-hook = "nav-menu-toggle"]')。on( 'click'、function(){{
$( '[data-hook = "nav-menu"]')。toggle();
}); <https:><https:>
カスタムjQuery関数でこれをさらに改善する:<p>
<https:><pre class="brush:php;toolbar:false">$。extend({{
フック:function(hookname){
$ $( `[data-hook〜 =" $ {hookname || '*'} "]`);
}
});
$ .hook( 'nav-menu-toggle')。on( 'click'、function(){
$ .hook( 'nav-menu')。toggle();
}); <code class="language-javascript"><https:>
<https:>この<p>フック<code>関数は、セレクターの使用法を簡素化し、単一の要素で複数のスペース分離フック名を可能にします。
<https:><https:>匿名関数の回避:<p> <strong>
名前付き関数を使用すると、読みやすさとテスト可能性が向上します
<https:><https:>関数togglenavmenu(){
$ .hook( 'nav-menu')。toggle();
}$ .hook( 'nav-menu-toggle')
<https:><https:>複数のイベントの取り扱い:<p><strong>
<https:> jQueryは複数のイベントを効率的に処理します:<https:>
<p><https:>$。hook( 'nav-menu-toggle')。 //m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712 sinone for sinone
$ .hook( 'nav-menu-toggle')。on({
「クリック」:trackclick、
「キーダウン」:TrackKeyDown、
「Mouseenter」:TrackMouseErter
}); //m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712 off()<code class="language-javascript">を使用したバインディングイベントは注意が必要です。名前空間を使用すると、無関係なハンドラーの偶発的な除去が防止されます:<https:>
<https:><p>//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712//m.sbmmt.com/link/29a9f8c8460e5e5e4edde557fd83712 with with with with with with with with with with with with with p.de557fd83712
$ .hook( 'click-track')。
//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712 dind with dind
$ .hook( 'click-track')。off( 'click.analytics');
//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712//m.sbmmt.com/link/29a9f8c8460e5e2be4edde557fd83712 remotic 'aynalytics
$( '*')。off( '。分析'); <code> <https:>
<https:><pre class="brush:php;toolbar:false">結論:<code class="language-javascript"><https:>
<https:>これらの手法は、より堅牢で保守可能な、テスト可能なjQueryイベント処理コードを作成します。 名前空間は、複雑なアプリケーションとプラグイン開発に特に価値があります。 <p>data-hook <strong>属性と名前付き関数を使用すると、コードの明確さが大幅に向上し、意図しない副作用のリスクが軽減されます。</strong></p></https:></https:></code></pre><div class="contentsignin">ログイン後にコピー</div></div></https:></https:>
以上がjQueryによる効果的なイベントバインドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。