目錄javascript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="box" class="box"> <p class="active">xjk1</p> <p class="active">xjk2</p> <input type="text"> </div> <script type="text/javascript" src="js/jquery.js"></script> <script> console.log($('.box'));//獲取jquery對象 </script> </body> </html> //jQuery.fn.init [div#box.box, prevObject: jQuery.fn.init(1)] 是一個僞數組
//jquery對象轉js節點對象 console.log($('#box')[0]);//jquery對象轉換js節點對象,索引方式 //js對象轉jquery對象 var box = document.getElementById('box'); console.log($(box)); console.log($('#box .active'));//打印出符合id=box,底下class=active全部jquery對象。
$('#box .active').click(function(){//點擊獲取事件 console.log(this);//this 指的是此時的p籤及其內容。 })
$('#box').css('color','red');
$('.box').css('color','red');
$('p').css('color','blue');
$('*').css({'outline':0,'border':0});//去除全部外框。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <span>span標籤1</span> <p>p標籤</p> <span> <span>span標籤2</span> </span> <div> <span>span標籤3</span> </div> </div> <script type="text/javascript" src="js/jquery.js"></script> <script> $("div span").css('color','red');//全部div 下的span標籤字體顏色都成爲紅色 </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <span>span標籤1</span> <p>p標籤</p> <span> <span>span標籤2</span> </span> <div> <p><span>span標籤3</span></p> </div> </div> <script type="text/javascript" src="js/jquery.js"></script> <script> $('div>span').css('color','red');//div下的全部span標籤 字體都變紅色 </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <p>p標籤</p> <span> <div>span標籤2</div> <div>span標籤3</div> <div>span標籤4</div> </span> </div> <script type="text/javascript" src="js/jquery.js"></script> <script> $('p+span').css('color','red');//p籤緊挨着的span籤的子標籤內容 </script> </body> </html> //span標籤2 span標籤3 span標籤4 都變紅
兄弟選擇器css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div>div標籤</div> <p>p標籤1</p> <p>p標籤2 <p>p標籤2的子標籤</p> </p> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> $('div~p').css('color','red'); </script> </body> </html> //p標籤字都稱爲紅色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> <li class="a"> <a href="#">caocao</a> </li> <li class="b">liubei</li> <li class="c">sunquan</li> <li class="d">guanyu</li> </ul> <script type="text/javascript" src="js/jquery.js"></script> <script> // console.log($('ul li:eq(1)'));//得到索引爲1的元素 // $('ul li:eq(1)').css('color','red');//改變索引爲1的元素的顏色 // console.log($('ul li:lt(3)'));//從0開始獲取索引小於3的元素。是僞數組 // $('ul li:odd').css('color','red');//更改索引爲奇數的文本顏色 $('ul li a:first').css('color','yellow');//第一個匹配元素更改顏色 $('ul li:last').css('color','grey');//更改最後一個元素的顏色 </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="nihao" class="a">href爲nihao</a> <a href="" class="b">href爲空</a> <a class="c">不包括href的a籤</a> <script type="text/javascript" src="js/jquery.js"></script> <script> // console.log($('a[href]'));//選擇包含href屬性元素 // console.log($("a[href='nihao']"));//選擇href='nihao'的元素 console.log($("a[href!='baidu']"));//選擇href!='baidu'的元素 </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> <li class="out1">1 <ul> <li class="inner1">內1</li> <li class="inner2">內2</li> <li class="inner3">內3</li> </ul> </li> <li class="out2">2</li> <li class="out3">3</li> </ul> <script type="text/javascript" src="js/jquery.js"></script> <script> // console.log($('ul').find('li'));//查找ul下全部li標籤,後代 // console.log($('.out1').children('ul'));//查找class=out1 的子標籤ul console.log($('ul').parent());//查找ul標籤的父標籤 </script> </body> </html>
console.log(document);//獲取文檔 console.log(document.body);//獲取body console.log(document.documentElement);//獲取html
//此爲JavaScript實現 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> button.active{ color: red; } p{ display: none; } p.active{ display: block; } </style> </head> <body> <button class="active">熱門</button> <button>電視影音</button> <button>電腦</button> <button>傢俱</button> <p class="active">熱門</p> <p>電視影音</p> <p>電腦</p> <p>傢俱</p> <script type="text/javascript"> // 1.獲取標籤 var btns = document.getElementsByTagName('button'); var ops = document.getElementsByTagName('p'); //2.給每一個標籤綁定點擊事件 //ES6能夠用let定義塊級做用域變量 for(let i = 0;i < btns.length; i++){ btns[i].onclick = function (){//排他思想 for(var j = 0; j < btns.length; j++){ btns[j].className = ''; ops[j].className = ''; } //改變button的樣式 this.className = 'active'; //改變p標籤的樣式 ops[i].className = 'active'; } } </script> </body> </html> //JQuery實現選項卡 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> button.active{ color: red; } p{ display: none; } p.active{ display: block; } </style> </head> <body> <button class="active">熱門</button> <button>電視影音</button> <button>電腦</button> <button>傢俱</button> <p class="active">家電</p> <p>電視影音</p> <p>電腦</p> <p>傢俱</p> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $('button').click(function(){ // 鏈式編程 //第二個按鈕 索引1 console.log($(this).addClass('active')); $(this).addClass('active').siblings('button').removeClass('active'); // 獲取當前點擊的元素的索引 console.log($(this).index()); $('p').eq($(this).index()).addClass('active').siblings('p').removeClass('active'); }) </script> </body> </html>
DOM屬性操做:對DOM元素的屬性進行讀取,設置和移除操做。好比prop()、removeProp()
類樣式操做:是指對DOM屬性className進行添加,移除操做。好比addClass()、removeClass()、toggleClass()
值操做:是對DOM屬性value進行讀取和設置操做。好比html()、text()、val()
設置屬性值或者返回被選元素的屬性html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id = 'box' class="boxs">你好</div> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> //獲取值:attr設置一個屬性值的時候,只是獲取值。獲取id值。 var id = $('div').attr('id'); console.log(id);//box //獲取class值 var cla = $('div').attr('class'); console.log(cla);//boxs //設置一個值 $('div').attr('id','thebox');//此時id值爲thebox //設置多個值 $('div').attr({'id':'manybox','class':'happy'}); </script> </body> </html>
移除屬性java
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id = 'box' class="boxs" title="say">你好</div> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> //移除一個屬性 $("#box").removeAttr('title'); //移除多個屬性,如移除class id屬性 $('#box').removeAttr('class id'); </script> </body> </html>
設置或者返回被選元素的屬性和值。jquery
當該方法用於返回屬性值時,則返回第一個匹配元素的值。編程
當該方法用於設置屬性值時,則爲匹配元素集合設置一個或多個屬性/值對。json
<body> <div id = 'box' class="boxs" title="say">你好</div> <span></span> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> //設置單個屬性和值 $('span').prop('class','like'); //設置多個屬性和值 $('span').prop({'title':'like','id':"line"}); </script> </body> </html>
attr() 和prop()的區別數組
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> 男<input type="radio" id='test' name="sex" checked/> 女<input type="radio" id='test2' name="sex" /> <button>提交</button> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript"> $(function(){ //獲取第一個input var el = $('input').first(); //undefined 由於attr是獲取的這個對象屬性節點的值,很顯然此時沒有這個屬性節點,天然輸出undefined console.log(el.attr('style')); // 輸出CSSStyleDeclaration對象,對於一個DOM對象,是具備原生的style對象屬性的,因此輸出了style對象 console.log(el.prop('style')); console.log(document.getElementById('test').style); $('button').click(function(){ alert(el.prop("checked") ? "男":"女"); }) }) </script> </body> </html>
爲每一個匹配的元素添加指定類名瀏覽器
$('div').addClass('box');//追加一個類名到原有的類名
還能夠爲匹配的元素添加多個類名app
$('div').addClass("box box2");//追加多個類名
例題
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> li.temp{ color:red; } </style> </head> <body> <ul type="none"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> $('li').addClass('temp');//爲每一個li標籤添加class='temp' </script> </body> </html>
從全部匹配的元素中刪除所有或者指定的類,移除指定類(1個或者多個)。
移除一個
<script> $('div').removeClass('box'); console.log($('div').attr('class'));//box2 </script>
移除所有
<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> $('div').removeClass(); console.log($('div').attr('class'));//'' </script>
小案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .active{ color:red; } </style> </head> <body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> //經過增長類值,改變其CSS渲染 $(function(){ $('ul li').click(function(){ $(this).addClass('active').siblings('li').removeClass('active'); }) }) </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span>123</span> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> //span標籤沒有class屬性,點擊以後span標籤添加class='active' $('span').click(function(){ $(this).toggleClass('active'); }) </script> </body> </html>
html() 是獲取選中標籤元素中全部的內容
$('#box').html();
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="box">123</div> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> //點擊div獲取事件,將123改成$('#box').html('<a href="https://www.baidu.com">百度一下</a>') $('div').click(function(){ $('#box').html('<a href="https://www.baidu.com">百度一下</a>'); }) </script> </body> </html>
text() 獲取匹配元素包含的文本內容。
語法:
$('#box').text();
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="box">123</div> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> $('#box').text("<a href='https://www.baidu.com'>百度一下</a>") </script> </body> </html>
獲取值:val()用於表單控件中獲取值,好比input textarea select等等。
語法:
$('input').val('設置表單控件值')
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="text" value="你好"> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script> $('input').val('替換表單的值'); </script> </body> </html>
方式1:
display: block;
實現的。$("div").show();
方式2:
$('div').show(3000);
方式3
$("div").show("slow");
方式4
//show(毫秒值,回調函數; $("div").show(5000,function () { alert("動畫執行完畢!"); });
示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> div{ width: 200px; height: 200px; background-color: red; display:none; } </style> </head> <body> <div></div> <script type="text/javascript" src="js/jquery.js"></script> <script> $('div').show(3000,function(){ alert('動畫執行完畢'); }); </script> </body> </html>
$(div).hide(); $(div).hide(1000); $(div).hide('slow'); $(div).hide(1000,function())
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> //css設置盒子寬高,背景色,display默認不顯示 #box{ width: 200px; height: 200px; background-color: red; display: none; } </style> </head> <body> <button id="btn">顯示</button> <div id="box"></div> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> //點擊鼠標獲取事件 $('#btn').click(function(){ if ($(this).text() === '顯示'){ //動畫顯示盒子 $('#box').show(1000,function () { //將按鍵文檔字改成‘隱藏’ $('#btn').text('隱藏'); }); }else{ //動畫隱藏盒子 $('#box').hide(1000,function () { //將按鍵文檔字改成‘顯示’ $('#btn').text('顯示'); }); } }) </script> </body> </html>
$('#btn').click(function(){ $('#box').toggle(3000,function(){ $(this).text('盒子出來了'); if ($('#btn').text()=='隱藏') { $('#btn').text('顯示'); }else{ $('#btn').text('隱藏'); } }); })
滑入
$('#box').slideDown(speed, 回調函數);
滑出
$('#box').slideUp(speed, 回調函數);
滑入滑出切換動畫
$('#box').slideToggle(speed, 回調函數);
淡入
$('#box').fadeIn(speed, 回調函數);
淡出
$('#box').fadeOut(speed, 回調函數);
淡入淡出
$('#box').fadeToggle('fast', callback);
語法:
$(selector).animate({params}, speed, callback);
做用:執行一組CSS屬性的自定義動畫。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> div { position: absolute; left: 20px; top: 30px; width: 100px; height: 100px; background-color: green; } </style> <script src="js/jquery.js"></script> <script> jQuery(function () { $("button").click(function () { var json = {"width": 500, "height": 500, "left": 300, "top": 300, "border-radius": 100}; var json2 = { "width": 100, "height": 100, "left": 100, "top": 100, "border-radius": 100, "background-color": "red" }; //自定義動畫 $("div").animate(json, 1000, function () { $("div").animate(json2, 1000, function () { alert("動畫執行完畢!"); }); }); }) }) </script> </head> <body> <button>自定義動畫</button> <div></div> </body> </html>