1.選擇器
1.1.id選擇器css
$('#id')
1.2.class選擇器jquery
$('.cl')
1.3.標籤選擇器markdown
<div></div> $('div')
1.4組合選擇器ide
<div id="i1"> <div class="c1"> <a>1</a> <a>2</a> <a>3</a> </div> </div> $('#i1,.c1,a ') #i1和.c1和a的全部標籤
1.5.層級選擇器函數
<div id="i1"> <div class="c1"> <a>1</a> <a>2</a> <a>3</a> </div> </div> $('#i1 .c1 a ') #i1下的.c1下的全部a標籤(包括子孫後代) $('#i1.c1>a ') #i1下的.c1下的全部a標籤(只有兒子代)
1.6.基本this
:first $('#i1 .c1 a:first') #i1下的.c1下的第一個a標籤 :last $('#i1 .c1 a:last') #i1下的.c1下的最後一個a標籤 :eq() $('#i1 .c1 a:eq(1)') #i1下的.c1下的最後一個a標籤
1.7.屬性3d
$('[name]') 具備name屬性的全部標籤 $('[name="user"]') name屬性等於user的標籤
1.8.篩選器code
<ul> <li></li><li></li><li id="i1"></li><li></li> </ul> $('i1').next() #下一個 $('i1').prev() #上一個 $('i1').parent() #父標籤 $('i1').children()#子標籤 $('i1').siblings()#兄弟標籤
2.操做實例blog
<body> <input type="button" value="全選" onclick="selectAll();"/> <input type="button" value="反選" onclick="reverseAll();"/> <input type="button" value="取消" onclick="cancleAll();"/> <table border="1"> <thead> <tr> <th>選項</th> <th>IP</th> <th>PORT</th> </tr> <tbody> <tr> <td><input type="checkbox" /></td> <td>1.1.1.1</td> <td>80</td> </tr> <tr> <td><input type="checkbox" /></td> <td>1.1.1.2</td> <td>80</td> </tr> <tr> <td><input type="checkbox" /></td> <td>1.1.1.3</td> <td>80</td> </tr> <tr> <td><input type="checkbox" /></td> <td>1.1.1.4</td> <td>80</td> </tr> <tr> <td><input type="checkbox" /></td> <td>1.1.1.5</td> <td>80</td> </tr> </tbody> </thead> </table> <script src="jquery-1.12.4.js"></script> <script> //全選 function selectAll() { //prop() 方法設置或返回被選元素的屬性和值 $(':checkbox').prop('checked',true); } //取消 function cancleAll() { $(':checkbox').prop('checked',false); } //反選 function reverseAll() { //each() 方法規定爲每一個匹配元素規定運行的函數 $(':checkbox').each(function () { //三元運算var v = 條件? 真值:假值 var v=$(this).prop('checked')?false:true; $(this).prop('checked',v); }) } </script> </body>
3.左側菜單展開實例ip
<head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> .left_menu{ height: 140px; width: 200px; border: 1px dashed #000000; } .item .head{ background-color: #000000; color: antiquewhite; } .item .content{ min-height:80px; } .hide{ display: none; } </style> </head> <body> <div class="left_menu"> <div class="item"> <div class="head">標題一</div> <div class="content">內容</div> </div> <div class="item"> <div class="head">標題二</div> <div class="content hide">內容</div> </div> <div class="item"> <div class="head">標題三</div> <div class="content hide">內容</div> </div> </div> <script src="jquery-1.12.4.js"></script> <script> //當點擊.head類標籤時觸發函數 $('.head').click(function () { //從當前點擊的標籤的下一個標籤移除hide類屬性 $(this).next().removeClass('hide'); //從當前點擊的標籤的父級標籤的兄弟標籤中找到包含.content類的標籤並添加hide類屬性 $(this).parent().siblings().find('.content').addClass('hide'); }) </script> </body>
4.自動添加
<head> <meta charset="UTF-8"> <title>Title</title> <style> .hide{ display: none; } .model{ position: fixed; top: 50%; left: 50%; width: 500px; height: 350px; margin-top: -200px; margin-left: -250px; background-color: aliceblue; z-index: 10; } .model p,h2{ text-align: center; } .model p input[type="text"]{ width: 300px; height: 28px; } .model p input[type="button"]{ width: 150px; height: 35px; } .shadow{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; background-color: black; opacity: 0.6; z-index: 9; } </style> </head> <body> <a onclick="addModel();">添加</a> <table border="1"> <thead> <tr> <th>地址</th> <th>端口</th> <th>操做</th> </tr> </thead> <tbody> <tr> <td>1.1.1.11</td> <td>80</td> <td><a class="edit">編輯</a>|<a>刪除</a></td> </tr> <tr> <td>1.1.1.12</td> <td>80</td> <td><a class="edit">編輯</a>|<a>刪除</a></td> </tr> <tr> <td>1.1.1.13</td> <td>80</td> <td><a class="edit">編輯</a>|<a>刪除</a></td> </tr> </tbody> </table> <div class="model hide"> <p><h2>操做</h2></p> <p>地址:<input type="text" name="host"/></p> <p>端口:<input type="text" name="port"/></p> <p><input type="button" value="取消" onclick="removeModel();"/></p> </div> <div class="shadow hide"></div> <script src="jquery-1.12.4.js"></script> <script> //打開添加框 function addModel() { $('.model,.shadow').removeClass('hide'); $('.model p input[type="text"]').val(''); } //關閉添加框 function removeModel() { $('.model,.shadow').addClass('hide'); } //點擊編輯時執行函數 $('.edit').click(function () { $('.model,.shadow').removeClass('hide'); //獲取當前點擊元素的父級標籤的全部兄弟標籤 var tds=$(this).parent().prevAll(); //取得標籤中的值,既innerText var port=$(tds[0]).text(); var host=$(tds[1]).text(); //給model賦值 $('.model p input[name="port"]').val(port); $('.model p input[name="host"]').val(host); }) </script> </body>
5.開關toggleClass
<head> <meta charset="UTF-8"> <title>Title</title> <style> .hide{ display: none; } </style> </head> <body> <input type="button" id="button1" value="按鈕"/> <div class="c1">dsajadw</div> <script src="jquery-1.12.4.js"></script> <script> //點擊#button1時觸發函數 $('#button1').click(function () { //調用toggleClass開關屬性 $('.c1').toggleClass('hide') }) </script> </body>