使用jquery的方法和技巧

來源:互聯網
上載者:User

標籤:

1、下載一個jquery.js的檔案

2、引入jquery.js檔案

1 <script type="text/javascript" src="__PUBLIC__/js/jquery-1.7.2.min.js"></script>2 <script type="text/javascript">3 $(function(){4 5 //這裡面使用jquery的方法6 7 8 });9 </script>

3、使用jquery提供的方法

學前,補習補習點jquery的方法。這裡,我從實用的角度去補習,不從基礎的角度去補習。因為基礎在jquery學習手冊上都有。

例子1.單擊html元素的事件(利用屬性level)

1 <input type="checkbox" name="access[]" id="app_1_1" value="1_1" level="1" />

jquery表達如下:

1 $(‘input[level=1]‘).click(function(){2 3 4 5 });

例子2.自主、任意地選擇html中的標籤

$(‘#id‘).siblings() 當前元素所有的兄弟節點
$(‘#id‘).prev() 當前元素前一個兄弟節點
$(‘#id‘).prevaAll() 當前元素之前所有的兄弟節點
$(‘#id‘).next() 當前元素之後第一個兄弟節點
$(‘#id‘).nextAll() 當前元素之後所有的兄弟節點
這三個方法都可以添加選取器,給出選擇條件,就能找到你指定的兄弟節點了。

如:

 

1 var inputs_all=$(this).parents(‘div.app‘).find(‘input‘);

$(this)表示當前點擊的對象;

.parents(‘div.app‘)表示對象的長輩中,<div class="app"></div>的那個html元素節點;

.find(‘input‘)表示找出對象下的所有html元素的<input />標籤。

所以,inputs_all是所有找出的<input />標籤的集合。

點擊頂層input時,選上,則所有都選上,沒選上,則所有都沒選上。

1 $(‘input[level=1]‘).click(function(){2     var inputs_all=$(this).parents(‘div.app‘).find(‘input‘);3     if ($(this).attr(‘checked‘)=="checked") {4         inputs_all.attr(‘checked‘,‘checked‘);5     }else{6         inputs_all.removeAttr(‘checked‘);7     }8 });

$(this).attr(‘checked‘)表示當前點擊對象的屬性值;

inputs_all.attr(‘checked‘,‘checked‘)表示讓inputs_all對象的所有<input />標籤添加一個checked屬性,並賦值為checked。即為<input checked="checked" />

inputs_all.removeAttr(‘checked‘)表示inputs_all對象的所有<input />標籤移除一個checked屬性。即為<input  />

這個最好自己練練

1 var all_born_children=$(this).parents(‘dt‘).next(‘span.method‘).find(‘input‘);2 var born_parent=$(this).parents(‘span.action‘).prev().find(‘input‘);3 var born_brothers=$(this).parents(‘dl‘).siblings().children(‘dt‘).find(‘input‘);

4、jquery的簡單調試方法

調試輸出代碼如下:將下列方法寫的jquery的點擊事件內

 1 var tbody = "";//調試代碼 2     $.each(born_brothers,function(n,value){ 3         alert(n+‘ ‘+value);//調試代碼 4         var trs = "";//調試代碼 5         trs += "<tr><td>" + value.checked +cunzai2+ "</td></tr>";//調試代碼 6         tbody += trs;//調試代碼 7     }); 8 $("#project").append(tbody);//調試代碼 9 //body標籤中寫輸出位置10 //<div id="project">在這裡輸出</div>

還要在<body>標籤中加

 1 <div id="project">在這裡輸出</div> 

jquery的全代碼如下:要開啟jquery調試,就把調試代碼最前面的//去掉

 1                 $(‘input[level=2]‘).click(function(){ 2                     var all_born_children=$(this).parents(‘dt‘).next(‘span.method‘).find(‘input‘); 3                     var born_parent=$(this).parents(‘span.action‘).prev().find(‘input‘); 4                     var born_brothers=$(this).parents(‘dl‘).siblings().children(‘dt‘).find(‘input‘); 5                      6                      7 //                    var tbody = "";//調試代碼 8                     var cunzai2=0; 9                         $.each(born_brothers,function(n,value){10                             11                             if(value.checked==true){12                                 cunzai2++;13                             }14                             //alert(n+‘ ‘+value);//調試代碼15 //                            var trs = "";//調試代碼16 //                            trs += "<tr><td>" + value.checked +cunzai2+ "</td></tr>";//調試代碼17 //                            tbody += trs;//調試代碼18                             19                         });20 //                            $("#project").append(tbody);//調試代碼21                     22 23                     if ($(this).attr(‘checked‘)=="checked") {24                         all_born_children.attr(‘checked‘,‘checked‘);25                         born_parent.attr(‘checked‘,‘checked‘);26                     }else{27                         all_born_children.removeAttr(‘checked‘);28                         if (cunzai2==0) {29                             born_parent.removeAttr(‘checked‘);30                         }else{31                             born_parent.attr(‘checked‘,‘checked‘);32                         }33                     }34 35                 });

現在練練手看看。有疑問,請聯絡:QQ郵箱:[email protected]

使用jquery的方法和技巧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.