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
08 - js frontend & jQuery - OpenWebSchool
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
openwebschool
August 16, 2012
Programming
300
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
08 - js frontend & jQuery - OpenWebSchool
openwebschool
August 16, 2012
Transcript
65 slides
網頁前端 & jQuery
目前瀏覽器裡跑的是 JS
修改網頁內容 從外部擷取資料 甚至存取 Webcam ... 可以做很多很多事情
但 JS 語言規範本身沒有輸入輸出
None
DOM Document Object Model
瀏覽器塞了一些野生的物件
所有的輸入輸出 都可以由這些野生的變數操作
console.log(‘hello’)
其中主要負責網頁內容的是
document
網頁上的東西 都和 document 的內容有對應關係
只要這物件有東西被修改 網頁也會隨之改變,反之亦然
困境
但是 DOM 這套 API 設計的實在太醜太難用了
document.getElementById
而且每套瀏覽器 實作的 API 都不盡相同 就是你,IE
使得網頁前端開發的 難度與維護成本大增
曙光
John Resig
2006 年釋出 jQuery 第一個版本
jQuery
前端 JS 函式庫
讓 DOM 操作輕鬆了許多 造福千千萬萬網頁前端開發者
jQuery 功能 & 特色
事件處理 特效 & 動畫 Ajax 跨瀏覽器 CSS 選擇器 DOM 遍歷
使用方法
去 jQuery 官網下載
<script src=”jquery.js”></script> <script src=”your-code.js”></script>
jQuery 會在全域塞入野生的物件 jQuery 或 $
將原本 DOM 物件傳入 $ 函式 就可以得到 jQuery 物件
選擇器
CSS 怎麼選就怎麼選
$(‘p’); $(‘#id’); $(‘.class’); $(‘p#id’); $(‘p a’);
操作 HTML 屬性
$(‘#a’).text(); // 取得 #a 文字 $(‘#a’).text(‘hello’); // 將 #a 文字設為
hello
$(‘#a’).attr(‘src’); // 取得 #a 的 src 屬性 $(‘#a’).attr(‘src’, ‘ha’); //
將 #a 的 src 設為 ha
$(‘#a’).addClass(‘banana’); // 新增 banana class $(‘#a’).removeClass(‘banana’); // 移除 banana class
$(‘#a’).hasClass(‘banana’); // 判斷 #a 是否有 banana 這個 class
$(‘#a’).html(); // 取得 #a 底下所有 html $(‘#a’).html(‘<p>haha</p>’); // 設定 #a
底下所有 html $(‘input#a’).val(); // 取得 #a 輸入的數值 $(‘input#a’).val(100); // 設定 #a 輸入的數值
事件處理
所有的輸入都是事件
監聽並且設置 callcack 函數 等事件觸發時做出響應
$(‘#a’).click(function () { console.log(‘#a clicked!’) }); // 當 #a 被點擊時即觸發綁定之
callback $(‘#a’).click(); // 主動觸發 #a 點擊事件
雙擊 滑鼠移上去 滑鼠移進去 壓下按鍵 滾動 大小改變 表單聚焦 表單送出 dblckick hover
mousein keydown scroll resize focus submit
$(‘#a’).click( callback ); $(‘#a’).on(‘click’, callback ); // 其實 click 等等方法都是
on 的簡寫 // on(事件名稱, callback) 全部通吃 $(‘#a’).off(‘click’, callback ); // 取消監聽 click 事件 $(‘#a’).one(‘click’, callback ); // 只監聽第一次 click 事件 $(‘#a’).trigger(‘click’); // 主動觸發 click 事件
$(document).ready(function () { console.log(‘ready to go!’); }); // 事件:DOM 載入完成
// 在 DOM 載完之前對 DOM 作操作可能會爆炸 // 所以等載完後再來做事情 // 做為程式初始化的起點,有點像 C 的 main $(function () { console.log(‘ready to go!’); }); // 因為每次都會用到,所以能簡寫成這樣
事件輸入會變成 callback 參數進來
$(‘#a’).mousemove(function (event) { console.log(event.offsetX); }); // 事件:滑鼠移動 // 從 event
傳入所有此事件相關資料 // 例如目前滑鼠的位置
DOM 遍歷
有時候光有選擇器還不夠 可能會需要在節點間遊走
$(‘#a’).parent().text(‘haha’); // 選取 #a 的父節點 $(‘#a’).prev().text(‘haha’); // 選取 #a 同一層但是在前面的所有節點
$(‘p’).eq(2).text(‘haha’); // 選取第三個選到的 p
CSS
$(‘#a’).css(‘margin’); // 取得 #a 的 margin 數值 $(‘#a’).css(‘margin’, 50); //
設定 #a 的 margin 數值
$(‘#a’).css({ margin: 100, width: 200, ‘font-size’: ‘200%’, fontWeight: ‘bold’ });
// 一次設定多筆樣式 // 無法表示的可以用字串表示
$(‘#a’).width(); // 取得 #a 的 width 數值 $(‘#a’).height(50); // 設定
#a 的 height 數值 $(‘#a’).offset(); // { top: ..., left: ... } // 取得 #a 的相對母元素位置 $(‘#a’).scrollTop(50); // 設定 #a 的滾動的數值
特效與動畫
$(‘#a’).hide(); // 隱藏 #a $(‘#a’).show(); // 顯示 #a
$(‘#a’).fadeIn(); // 淡入,預設花費時間為一秒 $(‘#a’).fadeOut(500); // 淡出,設定花費時間為半秒 $(‘#a’).fadeTo(0.5, 500); // 用半秒的時間讓透明度變成
0.5
$(‘#a’).slideDown(); // 滑下來,預設花一秒 $(‘#a’).slideUp(500); // 滑上去,設定花費時間為半秒
$(‘#a’).animate({ margin: 100, width: 200, ‘font-size’: ‘200%’, fontWeight: ‘bold’ },
500, function () { console.log(‘animation done’); }); // 花半秒時間變成指定的樣式 // 花費時間,easing,callback 都是可選
Ajax
原本瀏覽網頁擷取資料時 會需要切換然後重新載入頁面
現在改由 JS 在背後偷偷去 擷取,就不用更新整個頁面了
$.get(‘banana.json’, { data: 123 }, function (data) { console.log(‘data retrieved!’);
}, ‘json’); // 回傳的資料一樣是從 callback 的參數傳入 // 除了資源位址,傳送過去的資料,請求成功 // 的 callback 和傳送資料格式都是可選的 $.post( ... ); // 還有 post
$.ajax({ url: ‘banana/apple’, data: 100, error: function (error) { console.log(‘failed!’);
}, success: function (data) { console.log(‘success!’); } }); // $.get $.post 都是 $.ajax 的簡寫 // 有一大堆可選的東西可以填
More Decks by openwebschool
See All by openwebschool
11 - CodeIgniter - OpenWebSchool
openwebschool
1
390
09 - Node.JS - OpenWebSchool
openwebschool
1
400
07 - Javascript - OpenWebSchool
openwebschool
3
350
05 - MySQL - OpenWebSchool
openwebschool
1
280
06 - PHP & MySQL - OpenWebSchool
openwebschool
1
300
03 - PHP II - OpenWebSchool
openwebschool
2
410
04 - CSS - OpenWebSchool
openwebschool
4
370
01 - W3 intro - OpenWebSchool
openwebschool
3
260
02 - PHP I - OpenWebSchool
openwebschool
3
280
Other Decks in Programming
See All in Programming
Lean で手続き的プログラム の証明が書けるようになった
inoueasei
8
3.1k
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
840
Kotlin JVM用に作られたライブラリのKMPを前提とした再設計
jsoizo
0
240
AI駆動TDD ― 2026年10月版
hrmstrsmgs
0
110
Crash Auto Pipelines
veronikapj
1
450
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
190
Beyond Chatbots: Agentic UI with Open Standards @W-JAX 2026, München
manfredsteyer
PRO
0
130
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
160
眠らない開発チームの作り方
biwakonbu
0
130
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
220
SONY CISC-NEWS NWS-1750 + NWB-225 ハードウェアメンテとフレームバッファカラー対応 / KOF2026
tsutsui
0
150
Stage 3でも仕様は変わる。アプリケーション開発者のためのTC39プロセスの読み方
bicstone
1
280
Featured
See All Featured
Designing for Timeless Needs
cassininazir
1
570
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
610
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k
Evolving SEO for Evolving Search Engines
ryanjones
0
370
Building Applications with DynamoDB
mza
96
7.3k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
680
エンジニアに許された特別な時間の終わり
watany
109
250k
Why Our Code Smells
bkeepers
PRO
340
58k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
360
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
910
Practical Orchestrator
shlominoach
192
12k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k