ããã«ããã¹ãŠãå ã®èšäºã®èè ã«ä»£ãã£ãŠæžãããŠããŸãã
ã¯ããã«
jQueryã¯çŽ æŽãããã§ãã ã»ãŒ1幎é䜿çšããŠããŸããæåã¯éåžžã«æéãåããŸãããã䜿çšããã«ã€ããŠããŸãå éšã§ã®é 眮ã«ã€ããŠåŠã¶ã«ã€ããŠããŸããŸãæ°ã«å ¥ã£ãŠããŸãã
ç§ã¯jQueryã®å°é家ã§ã¯ãããŸããã ãããŠãããããªãããšã©ãŒã«ééããå Žåãç§ã¯ãµããããããšããããŸããã
ç§ã¯èªåããå¹³åçãªãjQueryãŠãŒã¶ãŒãšåŒãã§ããŸãããä»ã®èª°ãããã®1幎ã§ç¿åŸãããããããããããªãã¯ããããã³ãã¯ããã¯ããäœããèªãã§åŠã¶ããšã«èå³ããããšæããŸãã ãã®èšäºã¯åœåèããŠãããããã¯ããã«é·ãããšãããã£ããããæåã¯ããã²ãŒã·ã§ã³ãç°¡åã«ããããã®ãã³ã³ãã³ãããšãå¿ èŠã«å¿ããŠèå³ã®ãªãç¬éãã¹ãããã§ããããã«ããŸããã
å 容
- Google Codeãããã¬ãŒã ã¯ãŒã¯ãããŠã³ããŒããã
- ããŒãã·ãŒãã䜿çšãã
- ãã¹ãŠã®ã¹ã¯ãªãããçµã¿åãããŠããã¡ã€ã«ãµã€ãºãåæžããŸã
- Firebugã®ãã°æ©èœã䜿çšãã
- ãã£ãã·ã¥ãåªå ããŠãã§ããæäœãæå°éã«æãã
- DOMããªãŒã®æäœãæå°éã«æãã
- DOMãžã®æ¿å ¥ã«é¢ããŠã¯ããã¹ãŠãåäžã®èŠçŽ ã«ã©ããããŸã
- å¯èœãªå Žåãã¯ã©ã¹ã®ä»£ããã«ãidãã䜿çšããŸã
- ã»ã¬ã¯ã¿ãŒã®ã³ã³ããã¹ããèšå®ãã
- ã¡ãœããåŒã³åºãã·ãŒã±ã³ã¹ãè³¢ã䜿çšãã
- ã¢ãã¡ãŒã·ã§ã³ã®äœ¿çšæ¹æ³ãåŠã¶
- ã€ãã³ãã®ã¹ã±ãžã¥ãŒã«ãšå§ä»»ãåŠã³ãŸãã
- ã¯ã©ã¹ã䜿çšããŠç¶æ ãä¿åãã
- ããã«è¯ã-jQueryã§dataïŒïŒã¡ãœããã䜿çšããŠç¶æ ãä¿åãã
- ç¬èªã®ã»ã¬ã¯ã¿ãŒãæžã
- HTMLãæºåããããŒãžãèªã¿èŸŒãŸãããšãã«å€æŽããŸã
- ç¹å®ã®ã³ã³ãã³ãã«é 延èªã¿èŸŒã¿ã䜿çšããŠãå šäœçãªé床ãšSEOã®å©ç¹ãåŸã
- jQueryãŠãŒãã£ãªãã£é¢æ°ã䜿çšãã
- ãnoconflictãã䜿çšããŠãä»ã®ãã¬ãŒã ã¯ãŒã¯ã§äœ¿çšãããšãã«ã°ããŒãã«ãjqueryããªããžã§ã¯ãã®ååãå€æŽããŸã
- ã¢ããããŒããããŠããåçã確èªããæ¹æ³ã¯ïŒ
- åžžã«ææ°ããŒãžã§ã³ã䜿çšããŠãã ããã
- ã¢ã€ãã ãååšããããšã確èªããæ¹æ³ã¯ïŒ
- ãJSãã¯ã©ã¹ããhtmlãèŠçŽ ã«è¿œå ããŸã
- ããã©ã«ãã®åäœããªãŒããŒã©ã€ãããã«ã¯falseãè¿ããŸã
- ããã¥ã¡ã³ãæºåã€ãã³ãã®çããšã³ããª
1. Google Codeãããã¬ãŒã ã¯ãŒã¯ãããŠã³ããŒãããŸã
Googleã¯Google Codeã«ããã€ãã®JavaScriptã©ã€ãã©ãªãä¿æããŠããããµãŒããŒã«ä¿åããã®ã«æ¯ã¹ãŠããããããŠã³ããŒãããããšã«ã¯ããã€ãã®å©ç¹ããããŸãã ããã«ããããã©ãã£ãã¯ãç¯çŽããéåžžã«é«éã«ããŒãã§ããŸãããããŠæãéèŠãªããšã¯ããŠãŒã¶ãŒãGoogle CodeããããŠã³ããŒããããã®ã©ã€ãã©ãªã䜿çšããŠããããã®ãµã€ãã«ã¢ã¯ã»ã¹ããå Žåãã©ã€ãã©ãªã¯ãã§ã«ãã£ãã·ã¥ã«ãããŸãã
ããã¯å°ãªãããéèŠã§ãã ãã£ãã·ã¥ãããŠããªãjQueryã®åäžã®ã³ããŒãçæãããµã€ãã¯ããã€ãããŸããïŒ ãšãŠãç°¡åã§ã...
<script src="http://www.google.com/jsapi"></script> <script type="text/javascript"> // jQuery google.load("jquery", "1.2.6"); google.setOnLoadCallback(function() { // , , - . }); </script>
ãŸãã¯ããã®ãããªçŽæ¥ãªã³ã¯ãåã«æå¹ã«ããããšãã§ããŸã...
詳现ãªæé 㯠ãã¡ãã§ãã
2.ããŒãã·ãŒãã䜿çšãã
ã»ãšãã©ã®èšèªã«ã¯å€ãã®ããŒãã·ãŒããããããããã³ãã¯jQueryã«éå®ãããŸããã A4ãµã€ãºã®å°å·çšçŽã®ãã¹ãŠã®æ©èœãæå ã«çœ®ããŠãããšéåžžã«äŸ¿å©ã§ãã
http://www.gscottolson.com/weblog/2008/01/11/jquery-cheat-sheet/
http://colorcharge.com/jquery/
3.ãã¹ãŠã®ã¹ã¯ãªãããæ¥ç¶ãããã¡ã€ã«ãµã€ãºãå°ããããŸã
ããã¯ãJavaScriptã®äžè¬çãªã¢ããã€ã¹ã®1ã€ã§ãã ãã ãã mQueryãšmainã§jQueryã䜿çšãã倧èŠæš¡ãªãããžã§ã¯ãã¯ãããããå€ãã®ãã©ã°ã€ã³ã䜿çšããããïŒèè ã®ãµã€ãã§ã¯localScrollãlightboxãpreloadã䜿çšïŒ ããã®ã¢ããã€ã¹ã¯éåžžã«åœ¹ç«ã¡ãŸãã
ãã©ãŠã¶ã¯åæã«ã¹ã¯ãªãããããŒãã§ããŸããïŒå°ãªããšãããŸã ã§ã¯ãããŸããïŒãã€ãŸããäžåºŠã«è€æ°ã®ã¹ã¯ãªãããããŠã³ããŒãããŠããå ŽåãããŒãžå šäœã®ããŒãæéãççž®ãããŸãã ãããã£ãŠãéåžžã¯åããŒãžã«ã¹ã¯ãªãããããŒãããå¿ èŠãããããšãèæ ®ããŠãå±éåã«ã¹ã¯ãªããã1ã€ã®å€§ããªãã¡ã€ã«ã«åéããããšããå§ãããŸãã
äžéšã®ãã©ã°ã€ã³ã¯ãã§ã«æå°åããã³ããã±ãŒãžåã§ããŸãããæ®ãã®ã¹ã¯ãªãããçµåããå¿ èŠããããŸãã å人çã«ã¯ ããã«ãŒ ãã£ãŒã³ãšãã¯ãŒãºã 奜ã ã§ãã
4. Firebugã®ãã°æ©èœã䜿çšãã
FirebugããŸã ã€ã³ã¹ããŒã«ããŠããªãå Žå
å®å šãªèª¬æ ã¯æ¬¡ã® ãšããã§ãã
ç§ã®ãæ°ã«å ¥ãã®ãæ©èœãã¯ãã¢ã©ãŒããŠã£ã³ããŠã䜿çšããã«ç»é¢ã«ã¡ãã»ãŒãžãšå€æ°ãçŽæ¥è¡šç€ºã§ãããconsole.infoããšãéžæããã³ãŒãã«ã¿ã€ããŒãèšå®ããŠå®è¡ã«ãããæéã確èªã§ãããconsole.timeãã§ãããã®äœåã ãããŠãããã¯ãã¹ãŠéåžžã«äœ¿ããããã§ãã
console.timeïŒããªã¹ãã®äœæãïŒ; forïŒi = 0; i <1000; i ++ïŒ{ var myList = $ïŒ 'ãmyList'ïŒ; myList.appendïŒ 'ããã¯ãªã¹ãé ç®ã§ã' + iïŒ; } console.timeEndïŒ 'ãªã¹ãã®äœæ'ïŒ;
ãã®äŸã§ã¯ãéåžžã«åçŽã§æ¬è³ªçã«åœ¹ã«ç«ããªãã³ãŒããæå³çã«äœæããŸããã 以äžã®ãã³ãã®ããã€ãã§ã¯ãã¿ã€ããŒã䜿çšããŠæ¹åããæ¹æ³ã瀺ããŸãã
5.ãã£ãã·ã³ã°ãåªå ããŠãã£ãã·ã³ã°æäœãæå°éã«æãã
JQueryã»ã¬ã¯ã¿ãŒã¯çŽ æŽãããã§ãã ããŒãžã®èŠçŽ ãéåžžã«ç°¡åã«éžæã§ããŸããããå éšãã§ã¯é©åãªéã®ã¢ã¯ã·ã§ã³ãçæãããããããä¹±çšãããšå šäœçãªããã©ãŒãã³ã¹ãäœäžããå¯èœæ§ããããŸãã
åãèŠçŽ ãäœåºŠãïŒããšãã°ã«ãŒãã§ïŒéžæããå¿ èŠãããå Žåã¯ããã®èŠçŽ ã1åéžæããŠããã®å 容ãæäœããããã«ã¡ã¢ãªã«ä¿åããããšããå§ãããŸãã ã«ãŒãã䜿çšããŠç®æ¡æžããªã¹ãã«é ç®ãè¿œå ãã次ã®äŸãã芧ãã ããã
forïŒi = 0; i <1000; i ++ïŒ{ var myList = $ïŒ 'ãmyList'ïŒ; myList.appendïŒ 'ããã¯ãªã¹ãé ç®çªå·' + iïŒ; }
Firefox 3ã®ã³ã³ãã¥ãŒã¿ãŒã§ã¯1066ããªç§ããããŸãïŒIE6ã§ã©ã®ããã«æ©èœãããæ³åããŠã¿ãŠãã ããïŒïŒãããã¯JavaScriptã®æšæºã«ããéåžžã«é ãã§ãã 次ã«ãã»ã¬ã¯ã¿ãŒã1å䜿çšããå¥ã®äŸãèŠãŠã¿ãŸãããã
var myList = $ïŒ 'ãmyList'ïŒ; forïŒi = 0; i <1000; i ++ïŒ{ myList.appendïŒ 'ããã¯ãªã¹ãé ç®çªå·' + iïŒ; }
1è¡ããå€æŽããŠããªãã«ããããããã224ããªç§ããããããã4å以äžé«éã§ãã
6. DOMããªãŒã®æäœãæå°éã«æãã
æ¿å ¥ããDOMåŒã³åºãã®æ°ãæå°éã«æãããšãåã®ãã³ãã®ã³ãŒããããã«é«éåã§ããŸãã .appendïŒïŒã. prependïŒïŒãafterïŒïŒãwrapïŒïŒãªã©ã®DOMã§ã®æ¿å ¥æäœã¯ãªãœãŒã¹éçŽåã§ãããä¹±çšãããšäºæ ãæªåããå¯èœæ§ããããŸãã
å¿ èŠãªã®ã¯ãæååçµåã䜿çšããŠãªã¹ããäœæãã.htmlïŒïŒãªã©ã®é¢æ°ã1ã€ã ã䜿çšããŠãç®æ¡æžããªã¹ãã«ã¢ã€ãã ããã°ããè¿œå ããããšã ãã§ãã äŸãèŠãŠã¿ãŸãããïŒ
var myList = $ïŒ 'ïŒmyList'ïŒ; forïŒi = 0; i <1000; i ++ïŒ{ myList.appendïŒ 'ããã¯ãªã¹ãé ç®çªå·' + iïŒ; }
216ããªç§ããããŸãããããã¯1/5ç§ãå°ãäžåããŸããããªã¹ãã¢ã€ãã ãæååãšããŠäœæãã次ã®ããã«.htmlïŒïŒã¡ãœããã䜿çšããŠæ¿å ¥ãããš...
var myList = $ïŒ 'ãmyList'ïŒ; var myListItems = ''; forïŒi = 0; i <1000; i ++ïŒ{ myListItems + = '<li>ããã¯ãªã¹ãã¢ã€ãã çªå·ã§ãã' + i + '</ li>'; } myList.htmlïŒmyListItemsïŒ;
185ããªç§ããããŸãããããã»ã©é«éã§ã¯ãããŸããããçµå±ã¯31ããªç§ççž®ãããŸãã
7. DOMãžã®æ¿å ¥ã«é¢ããŠã¯ããã¹ãŠãåäžã®èŠçŽ ã«ã©ããããŸã
ããŠãããããªãæ©èœããã®ãç§ã«èããªãã§ãã ããïŒçµéšè±å¯ãªèª°ããããã説æã§ãããšç¢ºä¿¡ããŠããŸãïŒã
æåŸã®äŸã§ã¯ã.htmlïŒïŒã¡ãœããã䜿çšããŠã1000åã®ãªã¹ãé ç®ãç®æ¡æžããªã¹ãã«æ¿å ¥ããŸããã
æ¿å ¥ããåã«ããããulã¿ã°ã«å ¥ããå®æããulãå¥ã®ã¿ã°ïŒdivïŒã«å ¥ãããšããããã1000ã®ä»£ããã«1ã€ã®ã¿ã°ã®ã¿ãæ£åžžã«æ¿å ¥ãããããããå¹æããããŸãã ãã®ãããªãã®...
var myList = $ïŒ 'ãmyList'ïŒ; var myListItems = '<ul>'; forïŒi = 0; i <1000; i ++ïŒ{ myListItems + = '<li>ããã¯ãªã¹ãã¢ã€ãã çªå·ã§ãã' + i + '</ li>'; } myListItems + = '</ ul>'; myList.htmlïŒmyListItemsïŒ;
ä»åãããã¹ãŠã«é¢ãããã¹ãŠãã«ããã£ãæéã¯ããã19ããªç§ã§ãããã¯éåžžã«å€§ããªæ¹åã§ãããçç£æ§ã¯æåã®äŸãšæ¯èŒããŠ50å以äžåäžããŸããã
8.å¯èœãªå Žåãã¯ã©ã¹ã®ä»£ããã«ãidãã䜿çšããŸã
jQueryã§ã¯ãjavascriptããidãã§èŠçŽ ãéžæããã®ãšåããããç°¡åã«ã¯ã©ã¹ããšã«DOMèŠçŽ ãéžæããŸãããã®çµæãå€ãã®äººã ã以åãããã¯ããã«åºãã¯ã©ã¹ã䜿çšããŠããŸãã ãã ããjQueryã¯ä»»æã®ãã©ãŠã¶ãŒã®æšæºã¡ãœããïŒgetElementByIdïŒã䜿çšããDOMã§è¿œå ã®æäœãå®è¡ããªãããããidãã§èŠçŽ ãéžæããæ¹ãã¯ããã«åªããŠããŸãã ã©ããããããã§ããïŒ èŠãŠã¿ãŸãããã
åã®äŸã䜿çšããåãliãèŠçŽ ã«äžæã®ã¯ã©ã¹ãè¿œå ãããããã«å€æŽããŸãã 次ã«ãã«ãŒãå ã®ãã¹ãŠã®èŠçŽ ãéžæããŸãïŒäžåºŠã«1ã€ãã€ïŒã
//ãªã¹ããäœæããŸã var myList = $ïŒ 'ãmyList'ïŒ; var myListItems = '<ul>'; forïŒi = 0; i <1000; i ++ïŒ{ myListItems + = '<li class = "listItem' + i + '">ããã¯ãªã¹ãã¢ã€ãã çªå·ã§ã</ li>'; } myListItems + = '</ ul>'; myList.htmlïŒmyListItemsïŒ; //ã«ãŒãå ã®åèŠçŽ ãéžæããŸã forïŒi = 0; i <1000; i ++ïŒ{ var selectedItem = $ïŒ 'ãlistItem' + iïŒ; }
äºæ³ã©ããããã©ãŠã¶ã¯åŒ±ãããªãŒãºããã5066ããªç§ïŒ5ç§ä»¥äžïŒåŸã«ã®ã¿çµäºããŸããã ãã®ãããåèŠçŽ ã«ã¯ã©ã¹ã§ã¯ãªãidãæå®ããŠã³ãŒããå€æŽããã«ãŒãå ã§ãããããéžæããŸãããããã§ã«idã«ãã£ãŠéžæããŠããŸãã
//ãªã¹ããäœæããŸã var myList = $ïŒ 'ãmyList'ïŒ; var myListItems = '<ul>'; forïŒi = 0; i <1000; i ++ïŒ{ myListItems + = '<li id = "listItem' + i + '">ããã¯ãªã¹ãã¢ã€ãã ã§ã</ li>'; } myListItems + = '</ ul>'; myList.htmlïŒmyListItemsïŒ; //ã«ãŒãå ã®åèŠçŽ ãéžæããŸã forïŒi = 0; i <1000; i ++ïŒ{ var selectedItem = $ïŒ 'ïŒlistItem' + iïŒ; }
ä»åã¯ããã61ããªç§ã§ãã çŽ100åé«éã
9.ã»ã¬ã¯ã¿ãŒã®ã³ã³ããã¹ããèšå®ãã
ããã©ã«ãã§ã¯ã$ïŒ 'ãMyDiv'ïŒã®ãããªã»ã¬ã¯ã¿ãŒã䜿çšãããšãDOMããã¥ã¡ã³ãå šäœã«ã¢ã¯ã»ã¹ããŸããç¹å®ã®å ŽåïŒããšãã°ã倧ããªããŒãžã®å ŽåïŒãããã©ãŒãã³ã¹ã«å€§ããªåœ±é¿ãäžããå¯èœæ§ããããŸãã
jQueryé¢æ°ã¯ãã§ããæã«2çªç®ã®ãã©ã¡ãŒã¿ãŒãåããŸãã
<i> jQueryïŒåŒãã³ã³ããã¹ãïŒ</ i>
ã»ã¬ã¯ã¿ã«ã³ã³ããã¹ããæž¡ãããšã§ãæ€çŽ¢ãéå§ããèŠçŽ ãäŒããŸãã ãããã£ãŠãDOMããã¥ã¡ã³ãå šäœã«ã¢ã¯ã»ã¹ããå¿ èŠã¯ãããŸããã
ãããå®èšŒããããã«ãæåã®ãã³ãããäŸãå床åãäžããŸãã 1000åã®èŠçŽ ã®ç®æ¡æžããªã¹ããäœæããåèŠçŽ ã«ã¯ç¬èªã®ã¯ã©ã¹ããããŸãã 次ã«ãã«ãŒãå ã®ã¹ã¯ãªãããåèŠçŽ ãéžæããŸãã ã¯ã©ã¹ã§éžæããå Žåã1000åã®èŠçŽ ãã¹ãŠãéžæããã®ã«çŽ5ç§ããã£ãããšãæãåºããŠãã ããã
var selectedItem = $ïŒ 'ãlistItem' + iïŒ;
次ã«ãjQueryãç®æ¡æžããªã¹ãå ã§ã®ã¿ã»ã¬ã¯ã¿ãŒã䜿çšããããã«ã³ã³ããã¹ããè¿œå ããŸããã ãã®ããã«ïŒ
var selectedItem = $ïŒ 'ãlistItem' + iã$ïŒ 'ãmyList'ïŒïŒ;
3818ããªç§ããããŸããããããèªäœã¯ãŸã éåžžã«é ãã§ãããã»ã¬ã¯ã¿ãŒãžã®æå°éã®å€æŽã§æ¢ã«25ïŒ ä»¥äžã®é床åäžãå®çŸããŠããŸãã
10.ã¡ãœããåŒã³åºãã·ãŒã±ã³ã¹ã䜿çšãã
jQueryã®æãåªããæ©èœã®1ã€ã¯ãã¡ãœããåŒã³åºãã®ã·ãŒã±ã³ã¹ãäœæããæ©èœã§ãã ããšãã°ãèŠçŽ ã®ã¯ã©ã¹ãå€æŽããå ŽåïŒ
$ïŒ 'myDiv'ïŒãremoveClassïŒ 'off'ïŒãaddClassïŒ 'on'ïŒ;
ã»ãšãã©ã®å ŽåãjQueryã䜿çšããŠæåã®5åéã§ãããåŠç¿ããŸããããããã ãã§ã¯ãããŸããã ãŸããããã¯æ¹è¡ã§ããŸãæ©èœããŸãïŒjQueryã¯ãŸã JavaScriptã§ããããïŒãã€ãŸãã次ã®ãããªçŸããã³ãŒããæžãããšãã§ããŸãã
$ïŒãïŒmypanelãïŒ .findïŒ 'TABLE .firstCol'ïŒ .removeClassïŒ 'ãfirstCol'ïŒ .cssïŒãèæ¯ãïŒãèµ€ãïŒ .appendïŒ '<span>çŸåšããã®ã»ã«ã¯èµ€</ span>'ïŒ;
ã·ãŒã±ã³ã¹ã䜿çšããããšèªäœããã»ã¬ã¯ã¿ãŒã®äœ¿çšãå¶éããã®ã«åœ¹ç«ã¡ãŸãã
ããããããã ãã§ã¯ãããŸããã èŠçŽ ã«å¯ŸããŠããã€ãã®æ©èœãå®è¡ãããããæåã®äœ¿çšã®1ã€ãäœããã®æ¹æ³ã§èŠçŽ ãå€æŽãããšããŸããããããšãã°ïŒ
$ïŒ 'ïŒmyTable'ïŒãfindïŒ 'ãfirstColumn'ïŒãcssïŒ 'background'ã 'red'ïŒ;
ããŒãã«ãéžæãããfirstColumnãã¯ã©ã¹ãæã€ã»ã«ãèŠã€ããããã«ããã«èª¿æ»ããããããèµ€ã§è²ä»ãããŸããã
ã¯ã©ã¹ãlastColumnãã§ãã¹ãŠã®ã»ã«ãéã§çè²ããå¿ èŠããããšæ³åããŠãã ããã findïŒïŒé¢æ°ã䜿çšããããããfirstColumnãã¯ã©ã¹ã§ããŒã¯ãããŠããªããã¹ãŠã®ã»ã«ããã£ã«ã¿ãŒã§é€å€ãããããã»ã¬ã¯ã¿ãŒãåå©çšããŠããŒãã«èŠçŽ ãååŸããå¿ èŠããããããã«ã·ãŒã±ã³ã¹ã䞊ã¹ãããšã¯ã§ããŸãããïŒ ãããããjQueryã«ã¯endïŒïŒé¢æ°ãããã以åã®å ã®éžæã«æ»ããããã·ãŒã±ã³ã¹ãç¶è¡ã§ããŸãã
$ïŒ 'ïŒmyTable'ïŒ .findïŒ 'ãfirstColumn'ïŒ .cssïŒãèæ¯ãããèµ€ãïŒ .endïŒïŒ .findïŒ 'ãlastColumn'ïŒ .cssïŒ 'background'ã 'blue'ïŒ;
ã·ãŒã±ã³ã¹ã«å«ããããšãã§ããç¬èªã®jQueryé¢æ°ãäœæãããããç°¡åã§ãã å¿ èŠãªããšã¯ãèŠçŽ ãå€æŽããŠãããè¿ãé¢æ°ãæžãããšã ãã§ãã
$ .fn.makeRed = functionïŒïŒ{ return $ïŒthisïŒ.cssïŒ 'background'ã 'red'ïŒ; } $ïŒ 'ïŒmyTable'ïŒãfindïŒ 'ãfirstColumn'ïŒãmakeRedïŒïŒãappendïŒ 'hello'ïŒ;
ã¡ããã©ããïŒ
11.ã¢ãã¡ãŒã·ã§ã³ã®äœ¿çšæ¹æ³ãåŠã¶
jQueryãåããŠäœ¿ãå§ãããšããslideDownïŒïŒãfadeInïŒïŒãªã©ã®ã€ã³ã©ã€ã³ã¢ãã¡ãŒã·ã§ã³ã䜿çšããŠã¯ãŒã«ãªå¹æãç°¡åã«åŸãããããšã«æºè¶³ããŸããã jQueryã®animateïŒïŒã¡ãœããã¯éåžžã«äœ¿ãããããåæã«éåžžã«ã¯ãŒã«ãªã®ã§ãããå°ãå ãžé²ãã®ã¯éåžžã«ç°¡åã§ãã å®éãjQueryãœãŒã¹ãèŠããšããããã®ã¡ãœããã¯ãã¹ãŠãããæœè±¡åã®äžçš®ã§ãããanimateïŒïŒé¢æ°ã䜿çšããç¹æ®ãªã±ãŒã¹ã§ããããšãããããŸãã
slideDownïŒfunctionïŒspeedãcallbackïŒ{ this.animateãè¿ããŸãïŒ{heightïŒ "show"}ãspeedãcallbackïŒ; }ã fadeInïŒé¢æ°ïŒé床ãã³ãŒã«ããã¯ïŒ{ return this.animateïŒ{opacityïŒ "show"}ãspeedãcallbackïŒ; }
animateïŒïŒã¡ãœããã¯åçŽã«ä»»æã®CSSã¹ã¿ã€ã«ãåãã1ã€ã®å€ããå¥ã®å€ã«ã¹ã ãŒãºã«å€æããŸãã ãããã£ãŠãå¹ ãé«ããéæ床ãèæ¯è²ãã€ã³ãã³ããè²ããã©ã³ããµã€ãºãªã©ãå¿ èŠã«å¿ããŠå€æŽã§ããŸãã
ããã§ã¯ãããšãã°ãããŠã¹ããããŒãããšã¢ã€ãã ã®é«ãã100ãã¯ã»ã«ã«å¢å ããã¢ãã¡ãŒã·ã§ã³ã¡ãã¥ãŒãäœæããŸãã
$ïŒ 'ïŒmyList li'ïŒãmouseoverïŒfunctionïŒïŒ{ $ïŒããïŒ.animateïŒ{"é«ã"ïŒ100}ã "é ã"ïŒ; }ïŒ;
ä»ã®jQueryé¢æ°ãšã¯ç°ãªããã¢ãã¡ãŒã·ã§ã³ã¯èªåçã«ãã¥ãŒã«å ¥ãããããããæåã®çŽåŸã«2çªç®ã®åãå¹æãå¿ èŠãªå Žåã¯ãã¡ãœãããå床åŒã³åºãã ãã§ãã€ãã³ããšã³ãŒã«ããã¯ãå²ãåœãŠãå¿ èŠã¯ãããŸããã
$ïŒ 'ïŒmyBox'ïŒãmouseoverïŒfunctionïŒïŒ{ $ïŒããïŒ.animateïŒ{"width"ïŒ200}ã "slow"ïŒ; $ïŒããïŒ.animateïŒ{"é«ã"ïŒ200}ã "é ã"ïŒ; }ïŒ;
ãšãã§ã¯ããåæã«æµãããå Žåã¯ããªããžã§ã¯ãã®ãã©ã¡ãŒã¿ãŒã§äž¡æ¹ã®ã¹ã¿ã€ã«ãæå®ããŠã1ååŒã³åºããŸãã ãã®ãããªãã®ïŒ
$ïŒ 'ïŒmyBox'ïŒãmouseoverïŒfunctionïŒïŒ{ $ïŒthisïŒ.animateïŒ{"å¹ "ïŒ200ã "é«ã"ïŒ200}ã "é ã"ïŒ; }ïŒ;
ãã®ãããå¯å€æ°å€ãæã€ããããã£ãã¢ãã¡ãŒã·ã§ã³åã§ããŸãã ãŸããããã©ã«ãã§ã¯ãããè¡ããããªãããããã£ã®ã¢ãã¡ãŒã·ã§ã³åã«åœ¹ç«ã€ãã©ã°ã€ã³ãããŠã³ããŒãããããšãã§ããŸãã è²ãèæ¯è²ãªã© ã
12.ã€ãã³ãã®æå®ãšå§ä»»ãåŠã³ãŸãã
jQueryã§ã¯ãã€ãã³ããã³ãã©ãŒã¯åã«DOMèŠçŽ ã«å²ãåœãŠãããŸãããããã¯çŽ æŽãããããšã§ããããã³ãã©ãŒãè¿œå ãããããšããã©ãŒãã³ã¹ãäœäžããŸãã ã€ãã³ãåŠçã®å§ä»»ã«ãããæ©èœã®é¢ã§åæ§ã®çµæãæã€èŠçŽ ã«ãã£ãŠããªã¹ã³ããããã€ãã³ããè¿œå ããããšãã§ããŸãã ç解ããæè¯ã®æ¹æ³ã¯ã以äžã確èªããããšã§ã
$ïŒ 'ïŒmyTable TD'ïŒãã¯ãªãã¯ïŒé¢æ°ïŒïŒ{ $ïŒããïŒ.cssïŒ 'background'ã 'red'ïŒ; }ïŒ;
ãããã®ã»ã«ãã¯ãªãã¯ãããšãè¡šã®ã»ã«ãèµ€ã«å€ããç°¡åãªæ©èœã 10å50è¡ã®å°ããªããŒãã«ããããšããŸãããããã¯ã500åã®ã€ãã³ããã³ãã©ãŒãæ¢ã«ãåŸ æ©äžãã§ããããšãæå³ããŸãã ãã¶ããããŒãã«å šäœã«1ã€ã®ã€ãã³ãã®ã¿ãå²ãåœãŠãããŒãã«ã§ã¯ãªãã¯ãçºçãããšãã«ããã³ãã©ãŒã«ã€ãã³ããåŒãèµ·ãããã»ã«ã決å®ãããæ¹ãã¯ããã«é©åã§ããããïŒ
ããã¯ã€ãã³ãå§ä»»ãšåŒã°ãããã®ã§ãããå®è£ ã¯éåžžã«ç°¡åã§ãã
$ïŒ 'ïŒmyTable'ïŒãã¯ãªãã¯ïŒé¢æ°ïŒeïŒ{ var clicked = $ïŒe.targetïŒ; clicked.cssïŒ 'background'ã 'red'ïŒ; }ïŒ;
ãeãã«ã¯ãã¯ãªãã¯ãçºçããèŠçŽ ãªã©ãã€ãã³ãã«é¢ããæ å ±ãå«ãŸããŸãã 確èªããå¿ èŠãããã®ã¯ããŠãŒã¶ãŒãã¯ãªãã¯ããã»ã«ã確èªããããšã ãã§ãã ã¯ããã«äŸ¿å©ã§ãã
ã€ãã³ãã®å§ä»»ã«ã¯ãå¥ã®éåžžã«å€§ããªããŒãã¹ããããŸãã ãã³ãã©ãŒãèŠçŽ ã®ã»ããã«ãã€ã³ããããšãã€ãã³ããå²ãåœãŠããããšãã«ãã®ã»ããã«å«ãŸããŠããèŠçŽ ã ããçµåãããŸãã ã»ã¬ã¯ã¿ãŒã«ãã£ãŠéžæãããæ°ããèŠçŽ ãDOMã«è¿œå ããå Žåããããã«ã¯ãã³ãã©ãŒããããŸããã ç§ãåŸãŠãããã®ãåç §ããŠãã ããïŒ èŠçŽ ãå€æŽãããå Žåããããã¯åžžã«å²ãåœãŠãããåå²ãåœãŠãããªããã°ãªããŸããã
å§ä»»ã䜿çšããå Žåãå¿ èŠãªã ãèŠçŽ ãDOMã«è¿œå ã§ããã€ãã³ããã³ãã©ãŒã¯ãããã®èŠçŽ ã§æ£åžžã«æ©èœããŸãã
ãã®èšäºã®å·çæç¹ã§ã¯ãèè ã¯ããŒãžã§ã³1.2.6ã䜿çšããŠããŸããããããŒãžã§ã³1.3以éãjQueryã«ã¯ã€ãã³ãã®åå²ãåœãŠã®åé¡ã«å¯ŸåŠããæ°ããæ©èœã jQuery Event / liveããããŸãã
13.ã¯ã©ã¹ã䜿çšããŠç¶æ ãç¶æãã
ããã¯ãHTMLããŒã¹æ å ±ãä¿åããæãç°¡åãªæ¹æ³ã§ãã jQueryã¯ã¯ã©ã¹ã«åºã¥ããŠèŠçŽ ãå®å šã«æäœãããããèŠçŽ ã®ç¶æ ã«é¢ããæ å ±ãä¿åããå¿ èŠãããå Žåã¯ãä¿åããããã«è¿œå ã®ã¯ã©ã¹ãè¿œå ããŠã¿ãŸãããïŒ
以äžã«äŸã瀺ããŸãã ãã«ããŠã³ã¡ãã¥ãŒãäœæããŸãã ãã¿ã³ãã¯ãªãã¯ãããšãããã«ãéããŠããå Žåã¯slideDownïŒïŒã§è¡šç€ºãããéããŠããå Žåã¯slideUpïŒïŒã§è¡šç€ºãããããã«ãªããŸãã HTMLããå§ããŸãããã
<div class = "menuItem expanded"> <div class = "button"> ç§ãã¯ãªã㯠</ div> <div class = "panel"> <ul> <li>ã¡ãã¥ãŒé ç®1 </ li> <li>ã¡ãã¥ãŒé ç®2 </ li> <li>ã¡ãã¥ãŒé ç®3 </ li> </ ul> </ div> </ div>
ãšãŠãç°¡åã§ãïŒ ã©ãããŒèŠçŽ ïŒdivïŒã«è¿œå ã®ã¯ã©ã¹ãè¿œå ããŸããããã®ã¯ã©ã¹ã«ã¯ãèŠçŽ ã®ç¶æ ãäŒãã以å€ã®åœ¹å²ã¯ãããŸããã ãããã£ãŠãå¿ èŠãªã®ã¯ããã¿ã³ãæŒããããšãã«å¯Ÿå¿ããããã«ã®slideUpïŒïŒãŸãã¯slideDownïŒïŒãçæããonclickã€ãã³ããã³ãã©ã ãã§ãã
$ïŒ 'ãbutton'ïŒãã¯ãªãã¯ïŒé¢æ°ïŒïŒ{ var menuItem = $ïŒthisïŒ.parentïŒïŒ; var panel = menuItem.findïŒ 'ãpanel'ïŒ; ifïŒmenuItem.hasClassïŒ "expanded"ïŒïŒ{ menuItem.removeClassïŒ 'expanded'ïŒãaddClassïŒ 'collapsed'ïŒ; panel.slideUpïŒïŒ; } else ifïŒmenuItem.hasClassïŒ "collapsed"ïŒïŒ{ menuItem.removeClassïŒ 'collapsed'ïŒãaddClassïŒ 'expanded'ïŒ; panel.slideDownïŒïŒ; } }ïŒ;
ããã¯éåžžã«åçŽãªäŸã§ãããä»ã®ã¯ã©ã¹ãè¿œå ããŠãèŠçŽ ãŸãã¯HTMLã®äžéšã«é¢ããããããçš®é¡ã®æ å ±ãä¿åã§ããŸãã
ãã ããå°ãè€éãªå Žåã¯ãã¹ãŠã次ã®ãã³ãã䜿çšããããšããå§ãããŸãã
14.éåžžã«ã¯ãŒã«-jQueryã®çµã¿èŸŒã¿ããŒã¿ïŒïŒã¡ãœããã䜿çšããŠç¶æ ãä¿åãã
ç¹å®ã®çç±ã«ãããããã¯ååã«ææžåãããŠããŸããããjQueryã«ã¯å éšããŒã¿ïŒïŒã¡ãœããããããDOMèŠçŽ ã«åŸã£ãŠãããŒ/å€ã®ãã¢ã§æ å ±ãä¿åããããã«äœ¿çšã§ããŸãã ããŒã¿ã®ä¿åã¯æ¬¡ã®ããã«ç°¡åã§ãã
$ïŒ 'ïŒmyDiv'ïŒãdataïŒ 'currentState'ã 'off'ïŒ;
éå»ã®ãã³ãããäŸãæ¹åã§ããŸãã åãHTMLïŒãæ¡åŒµãã¯ã©ã¹ãé€ãïŒã䜿çšããdataïŒïŒé¢æ°ã䜿çšããŸãã
$ïŒ 'ãbutton'ïŒãã¯ãªãã¯ïŒé¢æ°ïŒïŒ{ var menuItem = $ïŒthisïŒ.parentïŒïŒ; var panel = menuItem.findïŒ 'ãpanel'ïŒ; ifïŒmenuItem.dataïŒ 'collapsed'ïŒïŒ{ menuItem.dataïŒ 'collapsed'ãfalseïŒ; panel.slideDownïŒïŒ; } ãã®ä»{ menuItem.dataïŒ 'collapsed'ãtrueïŒ; panel.slideUpïŒïŒ; } }ïŒ;
dataïŒïŒããã³removeDataïŒïŒã®è©³çŽ°ã«ã€ããŠã¯ã jQueryã®å éšæ§é ã«é¢ãããã®ããŒãžãåç §ããŠãã ãã ã
15.ç¬èªã®ã»ã¬ã¯ã¿ãŒãäœæãã
jQueryã«ã¯ãIDãã¯ã©ã¹ãã¿ã°ãå±æ§ãªã©ã§èŠçŽ ãååŸããããã®å€æ°ã®çµã¿èŸŒã¿ã»ã¬ã¯ã¿ãŒãå«ãŸããŠããŸãã ããããä»ã®åºæºã«åŸã£ãŠèŠçŽ ãéžæããå¿ èŠããããjQueryã«é©åãªããŒã«ããªãå Žåã¯ã©ãããã°ããã§ããããïŒ
æåã«é ã«æµ®ãã¶ã®ã¯ãæåã«èŠçŽ ã«ã¯ã©ã¹ãè¿œå ããããšã§ãããã®åŸãéžæã«ãããã䜿çšããŸãããå®éã«ã¯ãjQueryã®æ°ããã»ã¬ã¯ã¿ãŒãäœæããããšã¯ããã»ã©åé¡ã§ã¯ãããŸããã
ãããŸã§ã®ãšããããããå®èšŒããæè¯ã®æ¹æ³ã¯äŸã䜿çšããããšã§ãã
$ .extendïŒ$ãexpr ['ïŒ']ã{ over100pixelsïŒé¢æ°ïŒaïŒ{ return $ïŒaïŒ.heightïŒïŒ> 100; } }ïŒ; $ïŒ 'ãboxïŒover100pixels'ïŒãã¯ãªãã¯ïŒé¢æ°ïŒïŒ{ ã¢ã©ãŒãïŒãã¯ãªãã¯ã«ãã£ãŠåŠšããããèŠçŽ ãé«ã100ãã¯ã»ã«ä»¥äžãïŒ; }ïŒ;
ã³ãŒãã®æåã®ãããã¯ã¯ã100ãã¯ã»ã«ãè¶ ãããã¹ãŠã®èŠçŽ ãæ€çŽ¢ããç¬èªã®ã»ã¬ã¯ã¿ãŒãäœæããŸãã 2çªç®ã®ãããã¯ã¯ãæåã®ãããã¯ã䜿çšããŠãããããã¹ãŠã®èŠçŽ ã«ãã³ãã©ãŒãå²ãåœãŠãŸãã
ããã§ã¯ããã«ã€ããŠè©³ããã¯èª¬æããŸãããããã®ãããªããŒã«ãããã«åªããŠãããæ³åã§ããŸãããã«ã¹ã¿ã jqueryã»ã¬ã¯ã¿ãŒããšãããããã¯ãGoogleã§æ€çŽ¢ãããšãå€ãã®èå³æ·±ãããšãèŠã€ãããŸãã
16. HTMLãæºåããããŒãžãèªã¿èŸŒãŸãããå€æŽããŸã
ã¿ã€ãã«ã¯ããã»ã©æ確ã§ã¯ãªããããããŸãããããã®ãã³ãã¯
<div class = "fieldOuter"> <div class = "inner"> <div class = "field">ããã¯ãã£ãŒã«ãçªå·1ã§ã</ div> </ div> <div class = "errorBar"> <div class = "icon"> <img src = "icon.png" alt = "icon" /> </ div> <div class = "message"> <span>ããã¯ãšã©ãŒã¡ãã»ãŒãžã§ã</ span> </ div> </ div> </ div> <div class = "fieldOuter"> <div class = "inner"> <div class = "field">ããã¯ãã£ãŒã«ãçªå·2ã§ã</ div> </ div> <div class = "errorBar"> <div class = "icon"> <img src = "icon.png" alt = "icon" /> </ div> <div class = "message"> <span>ããã¯ãšã©ãŒã¡ãã»ãŒãžã§ã</ span> </ div> </ div> </ div>
ããã¯ãã©ãŒã ã¬ã€ã¢ãŠãã®äŸã§ãããã¿ã¹ã¯ã説æããããã«å°ãå€æŽãããŠããŸãã ãã®ã³ãŒãã¯ã²ã©ããã®ã§ããããã©ãŒã ã倧ããå Žåã¯ãã²ã©ãé·ãããŒãžã«ãªã£ãŠããŸããèŠèŠããããŒãžã«ãªã£ãŠããŸãããšã«åæããã§ãããã HTMLã§æ¬¡ã®ãããªãã®ã䜿çšããæ¹ãã¯ããã«è¯ãã§ãããã
<div class="field"> 1</div> <div class="field"> 2</div> <div class="field"> 3</div> <div class="field"> 4</div> <div class="field"> 5</div>
, â jQuery, HTML. :
$(document).ready(function() { $('.field').before('<div class="fieldOuter"><div class="inner">'); $('.field').after('</div><div class="errorBar"><div class="icon"> <img src="icon.png" alt="icon" /></div><div class="message"> <span> </span></div></div></div>'); }ïŒ;
, , , HTML-, .
17. « » (lazy loading) SEO
HTML, â «lazy loading»
. 3 , Google, . HTML load(), . :
$('#forms').load('content/headerForms.html', function() { // // . . }ïŒ;
. , . , , - , .
18. jQuery
jQuery â . , JavaScript:
jQuery utilities
, - ( IE7 indexOf()!). jQuery , , , .
JavaScript , , , . JavaScript «select», getElementById, «» , , . jQuery :
$('#selectList').val();
, jQuery .
19. «noconflict» «jquery»,
JavaScript- "$" , . , . .noconflict() "$" ,
var $jabrajabr = jQuery.noConflict(); $jabrajabr('#myDiv').hide();
20. ?
, , ( -, «» . .). , .
, â .load() «img» () callback-. «src» :
$('#myImage').attr('src', 'image.jpg').load(function() { alert(' '); }ïŒ;
, alert , .
21.
jQuery John Resig, , .
jQuery ( â . .) 1.2.6, , â Sizzle, , , Firefox, 4 . , â .
22. , ?
, , , jQuery , -, DOM. - , - , , length:
if ($('#myDiv).length) { // your code }
, .
23. «JS» «html»
Karl' Swedberg', jQuery.
âŠ
, jQuery ,
«JS» «html»:
$('HTML').addClass('JS');
, , JavaScript , CSS-, , JavaScript :
.JS #myDiv{display:none;}
ããã¯ãJavaScriptããªã³ã«ãªã£ãŠãããšãã«ã³ã³ãã³ããé衚瀺ã«ããå¿ èŠã«å¿ããŠjQueryã䜿çšããŠè¡šç€ºã§ããããšãæå³ããŸãïŒããšãã°ãäžéšã®ããã«ãæå°åãããŠãŒã¶ãŒãã¯ãªãã¯ãããšãã«ã®ã¿å±éããŸãïŒã ïŒæ€çŽ¢ããããå«ãïŒãé衚瀺ã§ã¯ãªãããããã¹ãŠã®ã³ã³ãã³ãã衚瀺ãããŸããç§èªèº«ããããç©æ¥µçã«äœ¿çšããŸãã
圌ã®å šæ㯠ãã¡ãã§èªãããšãã§ããŸãã
24.ããã©ã«ãã®åäœããªãŒããŒã©ã€ãããã«ã¯ãfalseãè¿ããŸãã
ããã¯æãããªå Žåãšããã§ãªãå ŽåããããŸããããããç¿æ £ãããå ŽåïŒ
<a href="#" class="popup">æªåœ¹ãã¯ãªãã¯ããŠãã ãã</a>
...ãããŠã次ã®ãããªã€ãã³ããã³ãã©ãå²ãåœãŠãŸãã
$ïŒ 'ãpopup'ïŒãã¯ãªãã¯ïŒé¢æ°ïŒïŒ{ //ãããã¢ããã®èµ·åã³ãŒã }ïŒ;
⊠, , , . , , , «» ( #).
, â , , . «return false;» , :
$('.popup').click(function(){ // popup' falseãè¿ããŸãã }ïŒ;
25.
, , $(document).ready.
:
$(document).ready(function (){ // }ïŒ;
:
$(function (){ // }ïŒ;
ããã¯ã仲éã®Jon Hobbs-Smithããã®åäºè©©jQuery ããªãã¯ã³ã¬ã¯ã·ã§ã³ã®çµããã§ããããªããããªãèªèº«ã®ããã«äœãæ°ããããšãåŠãã ããšãé¡ã£ãŠããŸãã
æŽæ°ãããé äžåãªã¹ãããç®æ¡æžããªã¹ããšæ£ããåŒãã§ãããNemeZZiZZ habruiserã«æè¬ããŸãïŒãé äžåãªã¹ãããšããŠç¿»èš³ããŸããããã¡ãããæåéãã§ãããééããªããã¥ã¯ããæŸã¡ãŸãïŒã
æŽæ°2ãã€ãã³ãã®äºçŽãšå§ä»»ã«é¢é£ããããŒãžã§ã³1.3以éã®jQueryã®æ°æ©èœãææããŠãããhabraiser david_mzã«æè¬ããŸãïŒã»ã¯ã·ã§ã³ 12ãåç §ïŒã