Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
使用 jQuery 的小技巧
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
aco wang
June 15, 2012
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
使用 jQuery 的小技巧
2012-06-11
aco wang
June 15, 2012
More Decks by aco wang
See All by aco wang
Rich Snippets
acowang
0
72
SEO 入門
acowang
0
58
網頁介面設計模式 - 送出邀約
acowang
1
330
Experience
acowang
0
87
洞察,開啟創新
acowang
0
54
如何把使用者經驗「做」出來
acowang
0
290
Other Decks in Programming
See All in Programming
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
440
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
610
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
230
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
130
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
150
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
700
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
160
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
310
cdk deploy JawsSonic #MARATHONしながらAWSリソースをデプロイしてみよう
akihisaikeda
2
100
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
56
38k
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
240
AI × TiDD / 2026.09.05 Redmine 大阪
tokudiro
1
140
Featured
See All Featured
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
490
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
Building the Perfect Custom Keyboard
takai
2
870
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
[SF Ruby Conf 2025] Rails X
palkan
2
1.4k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
520
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
240
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
500
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
300
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
300
Building Applications with DynamoDB
mza
96
7.2k
Transcript
使用 jQuery 的小技巧 aco 2012-06-11
Selectors
儘量使用 ID Selector • jQuery中最快的 Selector 是 ID Selector –
$(‟#someId‟) • 因為和 JavaScript 的原生函數 getElementById() 對應 • 從 ID Selector 開始繼承 – $(„#traffic_light input‟); <ul id=”traffic_light”> <li><input type=”radio” name=”light” value=”red” /> 紅色</li> <li><input type=”radio” name=”light” value=”yellow” /> 黃色</li> <li><input type=”radio” name=”light” value=”green” /> 綠色</li> </ul>
Tag Selector 和 Class Selector • 第二快的選擇器是 Tag Seletor –
$(‟head‟) • 因為和 JavaScript 的 getElementsByTagName() 對應 • 使用 Class Selector 搭配 Tag – $(„head.someClass‟) – $('div#someId‟) • 使用屬性選擇器,也可以使用 Tag 來修飾 – $('div[align=“left”]‟)
使用相關的子查詢 • 使用 children(), find(), filter() 等來進行子查詢 – $(„p‟).filter(„.highlight‟); •
使用子查詢相對較快速,ex: $parent.find(children),其 中的 $parent 已被 cache 住 • 使用 find() 取代 context $('.testdiv', '#pageBody'); $('#pageBody').find('.testdiv');
DOM
使用變數暫存 Select 後的 Object • 可以減少對 DOM 的尋訪 for (i
= 0; i < 1000; i++) { $('.myList‟).append('This is list item' + i); } var myList = $('.myList'); for (i = 0; i < 1000; i++) { myList.append('This is list item' + i); }
減少對 DOM 直接的操作 • 直接的DOM操作速度相對較慢 var myList = $('#myList'); for
(i=0; i<1000; i++){ myList.append('This is list item ' + i); } var myList = $('.myList'), myListItems = ''; for (i=0; i<1000; i++) { myListItems += '<li>This is list item ' + i + '</li>'; } myList.html(myListItems);
適當的使用鏈接 (Chaining) • 可以減少對 DOM Tree 的尋訪和程式碼的數量 var div =
$(„div‟), tempDiv; tempDiv = div.filter(„:first‟); tempDiv.css(„css‟, „red‟); tempDiv.attr(„name‟, „hello‟); $(„div‟).filter(„:first‟) .css(„color‟, „red‟) .attr(„name‟, „hello‟);
ready 與 load • $(document).ready 可以在 DOM 載入完成後再執行,但 如果要在完全載入完成後再執行的話,可使用 $(window).load
$(document).ready(function (){ // your code }); $(function (){ // your code });
this v.s $(this)
this • this,取得當前的元素 $('#textbox').hover( function() { this.title = 'Test'; }
}
$(this) • $(this),當前元素被 jQuery 處理的對象 $('#textbox').hover( function() { $(this).attr('title', 'Test');
} }
this v.s $(this) • 一般來說,在程式碼中盡量使用 $(this) ,可以讓程式碼 更有規範,在 jQuery 的函數也就可以直接使用,ex:
$(this).children() • $this = $(this);
Others
將程式碼寫成 jQuery Plugin • 如果要花一定得時間去開發一段 jQuery 程式碼,可以考慮將相 關的程式碼變成 Plugin •
能夠重用(Reuse)程式碼,並且能夠有效的組織程式碼 $(function(){ $.fn.yourPluginName = function(){ // Your code }; });
處理相鄰元素 $('#nav li').click(function(){ $('#nav li').removeClass('active'); $(this).addClass('active'); }); $('#nav li').click(function(){ $(this).addClass('active')
.siblings() .removeClass('active'); });
在 .css() 中傳入 Map $('button').on('click', function(){ box.css('font-size', '+=5') .css('color', 'white');
}); $('button').on('click', function(){ box.css({ 'fontSize': '+=5', 'color': 'white' }); });
JavaScript 原生函數相對較快 • for v.s each • createElement(„div‟) v.s $(<div></div>)
• this.id v.s $(this).attr('id')
判斷頁面是否太過複雜 • console.log( $('*').length ); • 返回的數值越小,表示網頁載入的速度越快,假如數值愈 大,可以考慮刪除無用或多餘的元素來優化程式碼
考慮未支援 JavaScript 的網頁 • 透過增加 Class 的方式將相關的元素隱藏或顯示 /* In JS
*/ $('.somthing').addClass(‟hideSomething'); /* In CSS */ .hideSomething {display:none;}
Thank You