1、jQuery至關於Python中的模塊,其誕生的目的就是簡化DOM操做。DOM操做基本上都封裝在jQuery中。不過也有例外,好比createElement建立標籤的操做,仍是須要DOM完成。Dom對象與jQuery對象可互相轉換css
jQuery對象 --------> Dom對象:$('#i1')[0]html
DOM對象------> jQuery對象:$(<dom對象>)python
2、參考網站:http://jquery.cuishifeng.cnjquery
3、版本號:編程
1.x 推薦使用這個,這是爲了兼容舊版本瀏覽器後端
2.x瀏覽器
3.x服務器
4、基本用法:app
jQuery的用法大體能夠分爲如下4步框架
一、下載jQuery
二、在HTML文件中,在body標籤最底部寫一個script的子標籤,其中寫一個src屬性導入jQuery
三、查找元素(使用時能夠以$開頭或jQuery)
四、操做元素
cond一、cond2爲標籤的條件
1、id選擇器:$('#<id>')
2、class選擇器:$('.<classname>')
3、標籤選擇器:$('<tagname>')
4、*:表明全部標籤
5、組合選擇器:$('a,.c1,#i10'),此選擇器會選擇a標籤、class爲c一、id爲i10的標籤。注意是用逗號分隔
6、層級選擇器:$('cond1 cond2'),查找cond1標籤下的符合cond2的全部標籤(子孫也查找),用空格分隔
$('parent>child'):查找parent下了子元素,而不查找孫子。
7、屬性選擇器:$('[name]') 查找具備name屬性的標籤
$(''[name='alex']") 查找name屬性爲alex的標籤
$('[attribute!=value]') 查找屬性不等於value的標籤
attribute^=value
attribute$=value
attribute*=value
[attrSet1][attrSet2][attrSetN]
8、表單相關:$('[:disabled]'):查找屬性爲disabled,好比text不能輸入的標籤
一、:first:$('a:first')至關於$('a').first() 找到第一個a標籤
二、:not(selector) 除了selector之外的
三、:even 奇數
四、:odd 偶數
五、:last:$('a:last')至關於$('a').last() 最後一個標籤
六、:eq(index):至關於$('.類名').eq(1) ($('.<classname>:eq(1)')) 找到索引爲index的標籤
七、:gt(index)
八、:lt(index)
九、:header 找到相似h1 h2 h3的標籤
1六、hasclass:$('a').hasclass(expr) 判斷在a標籤中找到有expr類的標籤
十、<jQuery對象>.find(...) 在jQuery對象中的子子孫孫查找
十一、<jQuery對象>.next() jQuery對象的下一下對象
nextAll()
nextUntil(expr) 直到找到expr
十二、<jQuery對象>.prev() jQuery對象的上一個對象
prevAll()
prevUntil()
1三、<jQuery對象>.parent 父親
parents 找到他的全部前輩
parentsUntil()
1四、<jQuery對象>.children 孩子
1五、<jQuery對象>.siblings 兄弟
紅色字體的返回的是DOM對象?
案例:全選、反選、取消
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="button" value="全選"> <input type="button" value="反選"> <input type="button" value="取消"> <table border="1"> <thead> <tr> <th>選項</th> <th>IP</th> <th>端口</th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox"> </td> <td>192.168.0.20</td> <td>808</td> </tr> <tr> <td> <input type="checkbox"> </td> <td>192.168.0.10</td> <td>9999</td> </tr> <tr> <td> <input type="checkbox"> </td> <td>192.168.0.30</td> <td>8080</td> </tr> <tr> <td> <input type="checkbox"> </td> <td>192.168.0.40</td> <td>80</td> </tr> </tbody> </table> <script src="jquery-1.12.4.js"></script> <script> // 全選 // 不用循環,會自動循環 $('[value="全選"]').click(function () { $(':checkbox').prop('checked', true); }) // 取消 $('[value="取消"]').click(function () { $(':checkbox').prop('checked', false); }) // 反選 // 利用DOM完成 // $('[value="反選"]').click(function () { // $(':checkbox').each(function () { // if(this.checked){ // this.checked=false; // }else{ // this.checked=true; // } // }) // }) // 利用jQuery完成 // $('[value="反選"]').click(function () { // $(':checkbox').each(function () { // if($(this).prop('checked')){ // $(this).prop('checked', false) // }else{ // $(this).prop('checked', true) // } // }) // }) // jQuery結合三元運算 $('[value="反選"]').click(function () { $(':checkbox').each(function () { var res = $(this).prop('checked')?false:true; $(this).prop('checked', res) }) }) </script> </body> </html>
知識點:
一、三元運算:res = condition?res1:res2;
condition成立返回res1,不然返回res2
二、在jQuery中,不須要使用循環,會自動循環,不然使用<jquery對象>.each()循環
<jQuery對象>.addClass(...) 添加樣式
<jQuery對象>.removeClass() 刪除樣式
<jQuery對象>.toggleClass(...) 樣式存在則刪除樣式,不存在則添加
<jQuery對象>.css(<name>,<val) 樣式中的某個屬性設置
案例以下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .hide{ display:none; } .content{ background-color: red; width:200px;height: 200px; } </style> </head> <body> <input type="button" value="開關"> <div class="content"></div> <script src="jquery-1.12.4.js"></script> <script> $('input[type="button"]').click(function () { $('.content').toggleClass('hide') }) </script> </body> </html>
案例:顯示菜單內容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .header{ background-color:blue; width:100px; } .hide{ display:none; } </style> </head> <body> <div class="item"> <div class="header">菜單1</div> <div class="content"> <div>內容1</div> <div>內容1</div> <div>內容1</div> <div>內容1</div> </div> </div> <div class="item"> <div class="header">菜單2</div> <div class="content hide"> <div>內容2</div> <div>內容2</div> <div>內容2</div> <div>內容2</div> </div> </div> <div class="item"> <div class="header">菜單3</div> <div class="content hide"> <div>內容3</div> <div>內容3</div> <div>內容3</div> <div>內容3</div> </div> </div> <div class="item"> <div class="header">菜單4</div> <div class="content hide"> <div>內容4</div> <div>內容4</div> <div>內容4</div> <div>內容4</div> </div> </div> <script src="jquery-1.12.4.js"></script> <script> $('.header').click(function () { // $(this).next().removeClass('hide') // $(this).parent().siblings().children('.content').addClass('hide') // 支持鏈式編程 $(this).next().removeClass('hide').parent().siblings().find('.content').addClass('hide') }) </script> </body> </html>
支持鏈式編程
一、jquery對象.prop(attr,val) 設置值
二、jquery對象.prop(attr) 獲取值
一、2專門用於checkbox和radio (這是因爲3版本如下的jQuery的問題)
三、<jQuery對象>.attr(name) 獲取屬性
四、<JQuery對象>.attr(name,val) 設置屬性
五、JQuery對象.removeAttr(name) 刪除屬性
一、jQuery對象.text() 獲取文本內容
二、jQuery對象.text(str) 設置文本內容
三、jQuery對象.html() 獲取HTML內容
四、jQuery對象.html(str) 設置HTML內容
五、jQuery對象.val() 獲取val值
六、jQuery對象.val(str) 設置val值
五、6主要是針對input系列的標籤,注意其中的select標籤使用五、6,若是是多選,5返回的是列表,6的參數要求是列表。
案例:模態對話框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .shadow{ position: fixed; top:0;bottom: 0;left: 0;right: 0; background-color: black; opacity: 0.5;z-index: 9; } .hide{ display: none; } .modal{ position: fixed; top:50%;left:50%; width:300px;height:300px; z-index: 10;background-color: white; margin-left:-150px;margin-top:-150px; } .modal .ip{ margin-top:20px; margin-left:20px; } .modal .port{ margin-top:20px; } .modal .confirm{ margin-top:10px; margin-right: 90px; text-align:right; } </style> </head> <body> <input type="button" value="添加" \> <table border="1"> <thead> <tr> <th>IP</th> <th>端口</th> <th>操做</th> </tr> </thead> <tbody> <tr> <td>192.168.10.10</td> <td>8080</td> <td> <a class="edit">編輯</a>|<a>刪除</a> </td> </tr> <tr> <td>192.168.10.20</td> <td>8088</td> <td> <a class="edit">編輯</a>|<a>刪除</a> </td> </tr> <tr> <td>192.168.10.30</td> <td>808</td> <td> <a class="edit">編輯</a>|<a>刪除</a> </td> </tr> </tbody> </table> <div class="shadow hide"></div> <div class="modal hide"> <div class="ip"> <span>IP</span> <input type="text" name="ip" \> </div> <div class="port"> <span>端口</span> <input type="text" name="port"\> </div> <div class="confirm"> <input type="button" value="肯定"> <input type="button" value="取消"> </div> </div> <script src="jquery-1.12.4.js"></script> <script> $('input[type="button"]').click(function () { $('.shadow,.modal').removeClass('hide') }) $('input[value="取消"]').click(function () { $('.shadow,.modal').addClass('hide') $('.modal .port input[name="port"]').val('') $('.modal .ip input[name="ip"]').val('') }) $('.edit').click(function () { tds = $(this).parent().prevAll() var port = $(tds[0]).text() var ip = $(tds[1]).text() $('.modal .port input[name="port"]').val(port) $('.modal .ip input[name="ip"]').val(ip) $('.shadow,.modal').removeClass('hide') }) </script> </body> </html>
模態對話框(改進版):爲了實現多列的擴展
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .shadow{ position: fixed; top:0;bottom: 0;left: 0;right: 0; background-color: black; opacity: 0.5;z-index: 9; } .hide{ display: none; } .modal{ position: fixed; top:50%;left:50%; width:300px;height:300px; z-index: 10;background-color: white; margin-left:-150px;margin-top:-150px; } .modal .ip{ margin-top:20px; margin-left:20px; } .modal .port{ margin-top:20px; } .modal .confirm{ margin-top:10px; margin-right: 90px; text-align:right; } </style> </head> <body> <input type="button" value="添加" \> <table border="1"> <thead> <tr> <th>IP</th> <th>端口</th> <th>操做</th> </tr> </thead> <tbody> <tr> <td target="ip">192.168.10.10</td> <td target="port">8080</td> <td> <a class="edit">編輯</a>|<a>刪除</a> </td> </tr> <tr> <td target="ip">192.168.10.20</td> <td target="port">8088</td> <td> <a class="edit">編輯</a>|<a>刪除</a> </td> </tr> <tr> <td target="ip">192.168.10.30</td> <td target="port">808</td> <td> <a class="edit">編輯</a>|<a>刪除</a> </td> </tr> </tbody> </table> <div class="shadow hide"></div> <div class="modal hide"> <div class="ip"> <span>IP</span> <input type="text" name="ip" \> </div> <div class="port"> <span>端口</span> <input type="text" name="port"\> </div> <div class="confirm"> <input type="button" value="肯定"> <input type="button" value="取消"> </div> </div> <script src="jquery-1.12.4.js"></script> <script> $('input[type="button"]').click(function () { $('.shadow,.modal').removeClass('hide') }) $('input[value="取消"]').click(function () { $('.shadow,.modal').addClass('hide') $('.modal .port input[name="port"]').val('') $('.modal .ip input[name="ip"]').val('') }) $('.edit').click(function () { var tds = $(this).parent().prevAll() tds.each(function () { var text = $(this).text() var target = $(this).attr('target') $('.modal input[name='+target+']').val(text) $('.shadow,.modal').removeClass('hide') }) // var port = $(tds[0]).text() // var ip = $(tds[1]).text() // $('.modal .port input[name="port"]').val(port) // $('.modal .ip input[name="ip"]').val(ip) // $('.shadow,.modal').removeClass('hide') }) </script> </body> </html>
案例:切換菜單,內容隨之改變(利用自定義屬性完成)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .hide{ display: none; } .menu{ width:980px;height:40px; background-color: #eeeeee; margin:0 auto; } .menu .menu-item{ float:left; padding: 10px; border-right:1px solid red; cursor: pointer; } .menu .active{ background-color: brown; } .content{ width:960px;min-height: 100px; margin:0 auto; padding:10px; } </style> </head> <body> <div class="menu"> <div class="menu-item" a="1">菜單1</div> <div class="menu-item" a="2">菜單2</div> <div class="menu-item" a="3">菜單3</div> </div> <div class="content"> <div b="1">內容1</div> <div class="hide" b="2">內容2</div> <div class="hide" b="3">內容3</div> </div> <script src="jquery-1.12.4.js"></script> <script> $('.menu .menu-item').click(function () { $(this).addClass('active'); $(this).siblings().removeClass('active'); var prop = $(this).attr('a'); $('.content div[b='+prop+']').removeClass('hide'); $('.content div[b='+prop+']').siblings().addClass('hide') }) </script> </body> </html>
案例:菜單切換,改進版(利用索引完成)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .hide{ display: none; } .menu{ width:980px;height:40px; background-color: #eeeeee; margin:0 auto; } .menu .menu-item{ float:left; padding: 10px; border-right:1px solid red; cursor: pointer; } .menu .active{ background-color: brown; } .content{ width:960px;min-height: 100px; margin:0 auto; padding:10px; } </style> </head> <body> <div class="menu"> <div class="menu-item">菜單1</div> <div class="menu-item">菜單2</div> <div class="menu-item">菜單3</div> </div> <div class="content"> <div>內容1</div> <div class="hide">內容2</div> <div class="hide">內容3</div> </div> <script src="jquery-1.12.4.js"></script> <script> $('.menu .menu-item').click(function () { $(this).addClass('active'); $(this).siblings().removeClass('active'); $('.content').children().eq($(this).index()).removeClass('hide').siblings().addClass('hide') }) </script> </body> </html>
知識點:
一、$(this).index 獲取this在父類標籤中的索引值
一、<jQuery對象>.append(<obj>) 在子標籤中尾部添加
二、<jQuery對象>.prepend(<obj>)(至關於<html_string).prepentto(jQuery對象)) 在子標籤頭部添加
三、<jQuery對象>.after(<obj>) 在標籤的後面添加
四、<jQuery對象>.before(<obj>) 在標籤以前添加
obj可爲jQuery對象、dom對象或字符串
五、<jQuery對象>.empty() 清空內容
六、<jQuery對象>.clear() 刪除整個標籤
七、<jQuery對象>.clone() 複製整個標籤
1、滑輪
一、$(window).scrollTop() 獲取window滑輪位置
二、$(window).scrollTop(val) 設置window滑輪位置
三、$(...).scrollTop() 獲取標籤滑輪位置
四、$(...).scrollTop() 設置標籤滑輪位置
scrollLeft
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div style="height:100px;overflow: auto;width:200px;"> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> <p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p><p>aaaaa</p> </div> <div style="height:2000px;"></div> <script src="jquery-1.12.4.js"></script> </body> </html>
2、offset 獲取標籤左上角在html中的位置
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="i1" style="height:100px;"></div> <div id="i2"></div> <script src="jquery-1.12.4.js"></script> </body> </html>
$(...).offset().left
$(...).offset().top
3、position():獲取子標籤在父標籤中的座標
一、<jQuery對象>.height() 獲取標籤內容的高度
二、<jQuery對象>.innerHeight() 獲取標籤內容的高度+2*padding高度
三、<jQuery對象>.outerHiehgt() 獲取標籤內容的高度+2*padding-top+2*border
四、<jQuery對象>.outerHeight() 獲取標籤內容的高度+2*padding-top+2*border+2*margin-top
一、<jQuery對象>.on(<event>,<func>)
二、<jQuery對象>.off(<event>,<func>)
如下綁定方式的底層都是基本一、2
三、<jQuery對象>.<event>(<func>)
四、<jQuery對象>.bind(<event>,<func>)
五、<jQuery對象>.unbind(<event>,<func>)
六、<jQuery對象>.delegate(<選擇器>,<event>,<func>)
七、<jQuery對象>.undelegate(<選擇器>,<event>,<func>)
第6種綁定方式是委託方式,意思就是此處的代碼不會立馬綁定事件,而是當用戶點擊時,搜索此標籤是否符合<jQuery對象>、<選擇器>,符合即綁定事件。
適用範圍:在Html添加新元素,而在新元素綁定事件
案例以下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="button" value="添加"> <input type="text"> <ul> <li>Treelight</li> <li>Alex</li> <li>Syvia</li> </ul> <script src="jquery-1.12.4.js"></script> <script> $('input[value="添加"]').click(function () { var new_obj = document.createElement('li'); $(new_obj).text($('input[type="text"]').val()) $('ul').append(new_obj) }); $('ul').delegate('li','click',function () { alert($(this).text()); }) </script> </body> </html>
案例以下(DOM方式):
JQuery方式
默認事件先執行的:checkobx
自定義事件先執行:a submit
$(<func>)
頁面框架是指標籤,但不指標籤的內容,好比圖片,這樣不用加載完圖片就能迅速綁定事件,
1、建立:$.extend(<fun_name>:function(){
...}
調用:$.<fun_name>
例子:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div></div> <script src="jquery-1.12.4.js"></script> <script> $.extend({ sum:function (a,b) { return a+b; } }); var v = $.sum(10,11); console.log(v); </script> </body> </html>
2、建立:
$.fn.extend(<fun_name>:function(){
...}
調用:
<jQuery對象>.<fun_name>
案例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div></div> <script src="jquery-1.12.4.js"></script> <script> $.fn.extend({ sum:function (a,b) { return a+b; } }); var v = $('div').sum(40,11); console.log(v); </script> </body> </html>
3、外部文件擴展
用法
一、<script src='path/to/file'></script>
二、$.<fun_name>
引用外部擴展的問題:
一、有可能函數名相同,無法解決
二、有可能全局變量同樣,可用自執行函數解決,把jQuery傳進去,如:。
(function(){
var status = 1;
arg.extend({
function ...})
// 封裝變量
})(jQuery)
設置select中的選項
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <select> <option>Scholes</option> <option>Kean</option> <option>Beckham</option> <option>Giggs</option> </select> <script src="jquery-1.12.4.js"></script> </body> </html>
綁定ctrl+鼠標左鍵事件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="button"> <script src="jquery-1.12.4.js"></script> <script> $('input').mousedown(function (e) { console.log(e.button,e.ctrlKey) }) </script> </body> </html>
option標籤不能綁定mousedown事件
關於select標籤綁定click事件:點擊標籤和選一個選項都會觸發click事件。咱們須要的是後者觸發的click事件。
================================== 驗證 ================================
JS: 驗證
各類驗證
$(':submit').click(function(){
$(':text,:password').each(function(){
...
return false;
})
return false;
})
後端:python實現
業務處理
....
初步感受也是提交數據,也有請求方式、數據、請求的URL,也會從服務器端返回數據,但不會自動刷新頁面
success:有數據返回時才執行
$(<form選擇器>).serialize()
url:請求地址
data:發送給服務器的數據
type:請求類型
dataType:指定從服務器端返回的數據類型
traditional:設爲true,則能夠給客戶端發送列表
error:請求錯誤時調用