posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉(zhuǎn)摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2009年2月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          1234567

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 860072
          • 排名 - 44

          最新評論

          閱讀排行榜

          第一章:入門

          $()取得頁面中的元素

          $(document).ready(function(){
           $('.poem-stanza').addClass('emphasized');
          });

          說明:

          $('.poem-stanza')       //取得頁面中添加了類poem-stanza的元素
          .addClass('emphasized');   //添加Css樣式,emphasized為類樣式
          .removeClass();
           
          第二章:選擇符

          $(document).ready(function(){
           //CSS選擇符
           $('#selectedplays>li').addClass('horizontal');          //查找id為selectedplays元素的頂層子元素li,添加樣式
           $('#selectedplays>li:not(.horizotal)').addClass('sublevel');    //查找id為selectedplays元素的頂層子元素li,并且沒有類horizotal 

           //XPath選擇符
           $('a[@href^="mailto:"]').addClass('mailto');           //查找錨元素中屬性href以“mailto:”開頭的元素
           $('a[@href$=".pdf"]').addClass('pdflink');            //查找錨元素中屬性href以“.pdf”結(jié)尾的元素
           $('a[@href*="mysite.com"]').addClass('mysite');        //查找錨元素中屬性href中包含“mysite.com”的元素(任意位置)

           //自定義選擇符
           $('tr:odd').addClass('odd');                   //查找奇數(shù)行
           $('tr:even').addClass('even');                  //查找偶數(shù)行
           //注::odd()和:even()選擇符使用的都是Javascript本身的從0開始得編號方式。表格的第1行編號為0(偶數(shù)),而第2行編號為1(奇數(shù))
           $('td:contains("Henry")').addClass('highlight');                   //查找td中包含"Henry"的

           //DOM遍歷方法
           $('th').parent().addClass('tableheading');           //查找th的父元素即標(biāo)題行
           $('tr:not([th]):even').addClass('even');              //查找tr元素中不包含子為th的,并且是偶數(shù)行
           $('tr:not([th]):odd').addClass('odd');          
           $('td:contains("Henry")').next().addClass('highlight');         //查找td中包含“Henry”的單元格的下一個單元格
           $('td:contains("Comedy")').siblings().addClass('highlight');  //查找td中包含“Comedy”的同級單元格
           $('td:contains("Comedy")').parent().find('td:gt(0)').addClass('highlight');
           //查找td中包含“Comedy”的父,再去查找td單元格編號大于0的單元格
           $('td:contains("Comedy)').parent().find('td').not(':contains("Comedy")').addClass('highlight');
           //查找td中包含“Comedy”的父,再去查找td單元格不包含“Comedy”的單元格

           //訪問DOM元素
           var tag = $('#myelement').get(0).tagName;
          });


          第三章:事件――扣動扳機(jī)

          綁定事件:

            .bind(‘click’,function(){})
            $(‘#switcher-large’).bind(‘click’,function(){…});給id為switcher-large的元素綁定click事件

            可簡寫為
            $(‘#switcher-large’).click(function(){…});

            方法:
            .toggle(function(){},function(){})

          接受兩個參數(shù)(都是函數(shù))第一次點(diǎn)擊調(diào)用第一個函數(shù),第二次點(diǎn)擊調(diào)用第二個
          $(‘#switcher h3’).toggle(function(){
                 $(‘#switcher .button’).addClass(‘hidden’);

          },function(){
                 $(‘#switcher .button’).removeClass(‘hidden’);

          });

          .toggleClass()是一種跟優(yōu)雅得方案,但對于交替執(zhí)行的操作而言,.toggle()的適用性更強(qiáng)

          $(‘#switcher h3’).click(function(){
                 $(‘#switcher .button’).toggleClass(‘hidden’); //自動檢查該類是否存在
          });

            .hover(function(){},function(){})

          接受兩個函數(shù)參數(shù),第一個指針進(jìn)入,第二個指針離開

          $(‘#switcher .button’).hover(function(){
                 $(this).addClass(‘hover’);
          },function(){
                 $(this).removeClass(‘hover’);
          });

            .unbind(‘click’)

          移除事件
            .trigger(‘click’)

          模擬用戶操作
          $(‘#switcher’).trigger(‘click’); 模擬點(diǎn)擊了一次switcher

          第四章:效果――為操作添加藝術(shù)性

          1.修改內(nèi)聯(lián)CSS

          語法:
          .css(‘property’,’value’);
          .css(‘property’:’value’,’property’:’value’);

          用法:
          var currentSize = $(‘div.speech’).css(‘fontSize’);//得到div.speech得字體大小
          var num = parasFloat(currentSize,10);  //將currentSize轉(zhuǎn)換為Float型
          var unit = currentSize.slice(-2); //返回從指定的字符開始的一個子字符串,-2為倒數(shù)第二個
          num *= 1.5;
          $(‘div.speech’).css(‘fontSize’,num+unit); //設(shè)置樣式

          2.基本的顯示和隱藏

          .show();
          .hide();

          用法:
          $(‘p:eq(1)’).hide(); //將第二個段落隱藏

          3.效果和速度
          指定顯示速度
          3種:slow、normal和fast 時間:0.6、0,4、0.2。也可以指定.show(850)
          $(‘p:eq(2)’).show(‘slow’);

          淡入和淡出

          .fadeIn();        //淡出
          .fadeOut();   //淡出

          $(‘p:eq(2)’).fadeIn(‘slow’);

          4.多重效果

          語句:.animate({param1:’value1’,parame2:’value2’},speed,function(){回調(diào)});

          用法:$(‘p:eq(1)’).animate({height:’show’,width:’show’,opacity:’show’},’slow’);

          這里使用簡寫的show將高度、寬度回復(fù)到被隱藏之前的值

          5.并發(fā)與排隊(duì)效果
          處理一組元素
          $(‘div.buttont’).animate({left:650},’slow’).animate({height:38},’slow’);
          先移動到left為650的位置,在將高度設(shè)置為38
          $(‘div.button’)
                 .fadeTo(‘slow’,0.5)                            //先淡化
                 .animate({left:650},’slow’)           //在移動
                 .fadeTo(‘slow’,1.0)                            //在淡出
                 .slideUp(‘slow’)                          //最后滑到上方隱藏

          注意:排隊(duì)不適用于其他的非效果方法,例如:.css()
          處理多組元素

          $(‘p:eq(2)’).slideUp(‘slow’).next().slideDown(‘slow’); 段落3向上滑出,同時段落4向下滑入

          回調(diào)函數(shù):

          Var $thisPara = $(‘p:eq(2)’);
          $(‘p:eq(2)’).next().slideDown(‘slow’,function(){

                 $thisPara.slideUp(‘slow’);

          });

          段落4下滑完成后,段落3才開始上滑

          第五章:DOM操作-基于命令改變頁面

          1.操作屬性
          非css屬性

          $(document).ready(function(){
              $('div.chapter a[@href*=wikipedia]').each(function(index){
                  var $thisLink = $(this);
                  $thisLink.attr({
                      'rel': 'external',
                      'id': 'wikilink-' + index,
                      'title': 'learn more about ' + $thisLink.text() + ' at wikipedia'
                  });
              });
          });

          //遍歷div.chapter 下面所以錨元素href中包含wikipedia,為其添加屬性
          深入理解$()工廠函數(shù)
          $('<a href="#top">back to top</a>');    創(chuàng)建新元素

          2.插入新元素

          .insertBefore()
          .before()

          作用相同,區(qū)別取決于將它們與其他方法進(jìn)行連綴

          .insertAfter()
          .after()

          // $('div.chapter p').after('<a href="#top">back to top</a>');

          // $('div.chapter p').before('<a href="#top">back to top</a>');

          $('<a href="#aaa">back to top</a>').insertAfter('div.chapter p');

          $('<a id="aaa" name="top"></a>').prependTo('body');

          .perpendTo插入了作為目標(biāo)的錨

          // $('<a href="#top">back to top</a>').insertBefore('div.chapter p');

          3.移動元素

          $('span.footnote').insertBefore('#footer');
          將span中類為footnote的插入到id為footer的前面
          標(biāo)注、編號和鏈接到上下文
              $('span.footnote').each(function(index){
                  $(this)
                      .before('<a href="#foot-note-' + (index+1) +
                              '" id="context-' + (index+1) +
                              '" class="context"><sup>' + (index+1) +
                              '</sup></a>');
              });
          $('span.footnote').insertBefore('#footer');

          遍歷span.footnote,在各個元素前加標(biāo)注后,在將span.footnote添加到#footer前

          插入腳注

          $('<ol id="notes"></ol>').insertAfter('div.chapter');
          $('span.footnote').each(function(index){
                  $(this)
                      .before('<a href="#foot-note-' + (index+1) +
                              '" id="context-' + (index+1) +
                              '" class="context"><sup>' + (index+1) +
                              '</sup></a>')
                      .appendTo('#notes')
                      .append('&nbsp;(<a href="#context-'+(index+1)+'">context</a>)');   
          });

          先創(chuàng)建一個ol元素notes,并添加到div.chapter后面,遍歷span.footnote,先添加標(biāo)注,然后通過appendTo其添加到ol末尾,最后又通過append添加一個錨元素。

          4.包裝元素

          .wrap()
          .wrap('<li id="foot-note-' + (index+1) + '"></li>')

          5.復(fù)制元素

          .clone()

          $('div.chapter p:eq(0)').clone().insertBefore('div.chapter');

          將div.chapter中得第一段復(fù)制后插入在div.chapter前面

          深度復(fù)制

          $('div.chapter p:eq(0)').clone(false)

          只復(fù)制結(jié)構(gòu),內(nèi)部的文本沒有復(fù)制過來,因?yàn)槲谋疽彩荄OM節(jié)點(diǎn)

          通過復(fù)制創(chuàng)建突出引用

          var $parentParagraph = $(this).parent('p');

          $parentParagraph.css('position','relative');

          $(this).clone()

          .addClass('pulled')

          .prependTo($parentParagraph);

          修飾突出引用

          $clonedCopy.addClass('pulled')
          .find('span.drop')
              .html('&hellip;')
              .end()
              .prependTo($parentParagraph)
              .wrap('<div class="pulled-wrapper"></div>');

          var clonedText = $clonedCopy.text();

          $clonedCopy.html(clonedText);

          第六章:AJAX 讓網(wǎng)站與時俱進(jìn)

          1.基于請求加載數(shù)據(jù)
          追加HTML

          //載入遠(yuǎn)程 HTML 文件代碼并插入至 DOM 中

          $('#dictionary').load('a.html',function(){
                Alert(“加載完畢!”);//回調(diào)函數(shù)
          })

          操作JavaScript對象

          JSON:
          [
          {
              "term": "BACKBITE",
              "part": "v.t.",
              "definition": "To speak of a man as you find him when he can't find you."
            },
            {
              "term": "BEARD",
              "part": "n.",
              "definition": "The hair that is commonly cut off by those who justly execrate the absurd Chinese custom of shaving the head."
            }
          ]

          JQuery:

          $.getJSON('b.json',function(data){ //通過 HTTP GET 請求載入 JSON 數(shù)據(jù)
                      $.each(data,function(entryIndex,entry){
                          var html = '<div class="entry">';
                          html += '<h3 class="term">' + entry['term'] + '</h3>';
                          html += '<div class="pare">' + entry['part'] + '</div>';
                          html += '<div class="definition">';
                          html += entry['definition'];
                          if(entry['quote']){
                              html += '<div class="quote">';
                              $.each(entry['quote'],function(lineIndex,line){
                                  html += '<div class="quote-line">' + line + '</div>';
                              });
                              if(entry['author']){
                                  html += '<div class="quote-author">' + entry['author'] + '</div>';
                              }
                              html += '</div>';
                          }
                          html += '</div>';
                          html += '</div>';
                      });

          執(zhí)行腳本

          通過 HTTP GET 請求載入并執(zhí)行一個 JavaScript 文件

          $.getScript('c.js',function(){
              Alert(“加載完畢”);//回調(diào)函數(shù)
          });

          加載XML文檔

          //通過遠(yuǎn)程 HTTP GET 請求載入信息。

          $.get('d.xml',function(data){
                  $(data).find('entry').each(function(){
                          var $entry = $(this);
                          var html = '<div class="entry">';
                          html += '<h3 class="term">' + $entry.attr('term') + '</h3>';
                          html += '<div class="part">' + $entry.attr('part') + '</div>';
                          html += '<div class="definition">'
                          html += $entry.find('definition').text();
                          var $quote = $entry.find('quote');
                          if($quote.length){
                              html += '<div class="quote">';
                              $quote.find('line').each(function(){
                                  html += '<div class="quote-line">' + $(this).text() + '</div>';                    
                              });
                              if($quote.attr('author')){
                                  html += '<div class="quote-author">' + $quote.attr('author') + '</div>';
                              }
                              html += '</div>';
                          }
                          html += '</div>';
                          html += '</div>';
                          $('#dictionary').append($(html));
                      });
                  });

          2.選擇數(shù)據(jù)格式

          3.向服務(wù)器傳遞數(shù)據(jù)

          4.關(guān)注請求

          方法:

          .ajaxStart()
          .ajaxStop()

          當(dāng)AJAX請求開始且尚未進(jìn)行其他傳輸時,會觸發(fā).ajaxStart()的回調(diào)函數(shù),相反,當(dāng)最后一次活動請求終止時,則會執(zhí)行通過.ajaxStop()注冊的回調(diào)函數(shù)。

          示例:  

          //當(dāng)請求時,顯示#loading,結(jié)束時,隱藏loading

          $(document).ready(function(){
          $('#loading').ajaxStart(function(){
               $(this).show();
          }).ajaxStop(function(){
               $(this).hide();
          });
          });

          5.Ajax和事件

          限制函數(shù)綁定的作用域

          $(document).ready(function(){
          var bindBehaviors = function(scope){
               $('h3',scope).click(function(){
                   $(this).toggleClass('highlighted');
               });
          }; 

          bindBehaviors(this);

          //這里的綁定作用域是document

          $(document).ready(function(){
               $('#letter-a .button').click(function(){
                   $('#dictionary').hide().load('a.html',function(){
                       bindBehaviors(this);
          //這里的是文檔中的所以<h3>元素
                       $(this).fadeIn();
                   });
               });
          });

          });

          6.安全限制

           

          posted on 2009-02-14 20:25 周銳 閱讀(532) 評論(0)  編輯  收藏 所屬分類: Ajax
          主站蜘蛛池模板: 巴中市| 长兴县| 界首市| 松潘县| 庆阳市| 兴安县| 葫芦岛市| 青田县| 鲁甸县| 敦化市| 同德县| 常熟市| 栾川县| 赫章县| 苗栗县| 奈曼旗| 上饶县| 宣威市| 平乡县| 铜陵市| 铜鼓县| 松溪县| 清流县| 盘锦市| 永泰县| 滕州市| 肥西县| 化德县| 聂拉木县| 韩城市| 东平县| 洞头县| 若羌县| 宜都市| 沿河| 民县| 神木县| 云和县| 新营市| 蓬莱市| 开江县|