jq操做頁面文檔
http://jquery.cuishifeng.cn/css
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jq初始</title> </head> <body> <h1>jQuery就是js的工具庫 - 一系列功能的集合體</h1> <h2>jq內部語法採用的就是原生js</h2> <h2>jq環境如何搭建 - 在須要使用jq的html中引入jquery.js便可</h2> <h2>jq就是優化了原生js魚頁面進行交互的邏輯</h2> </body> <!-- CDN 鏈接 外部資源 --> <!--<script src="https://code.jquery.com/jquery-3.4.1.js"></script>--> <!--<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>--> <script src="js/jquery-3.4.1.js"></script> <script> // jQuery對象 console.log(jQuery); console.log($); console.log(Owen); console.log($('h1')); $('h1').click(function () { $('h1').css('color', 'red') }) </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <div id="d" class="box"></div> <input type="text" id="d2" class="box" /> <h3 class="h3"></h3> </body> <script src="js/jquery-3.4.1.min.js"></script> <script> // jq選擇器:$('css選擇器語法') let $div = $('#d'); console.log($div); let $boxs = $('.box'); console.log($boxs); // jq對象如何轉換爲js對象 - jq對象能夠理解爲裝有js對象的數組 // 就是經過索引取值 let div = $div[0]; console.log(div); console.log(document.getElementsByClassName('box')[0]); console.log(document.querySelectorAll('.box')[0]); console.log($div[0]); console.log($boxs[0]); console.log($boxs[1]); // js如何轉換爲jq對象 let $newDiv = $(div); console.log($newDiv); </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jq事件</title> <style> .box { width: 200px; height: 200px; background-color: orange; margin-bottom: 10px; } </style> </head> <body> <div class="box">1</div> <div class="box">2</div> </body> <script src="js/jquery-3.4.1.min.js"></script> <script> let $box = $('.box'); // $box.click(function () { // // this就是被點擊的標籤 - js對象 // console.log(this); // console.log($(this)); // }); // jq對象能夠完成事件的批量綁定 $box.on('click', function () { console.log(this); console.log(this.innerText); console.log($(this)); }); // js必須手動循環 綁定 // let boxs = document.querySelectorAll('.box'); // for (box of boxs) { // box.onclick = function () { // console.log(this); // console.log($(this)); // } // } </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jq內容操做</title> </head> <body> <h1 class="title">標題</h1> <input type="text" class="title"> <button class="btn">改標題</button> </body> <script src="js/jquery-3.4.1.min.js"></script> <script> // js - jsObj.value | jsObj.innerText | jsObj.innerHTML // jq - jqObj.val() | jqObj.text() | jqObj.html() // 取值:jqObj.text() | jqObj.text('新值') | jqObj.text(fn) let $btn = $('.btn'); let $inp = $('input.title'); let $h1 = $('h1.title'); $btn.click(function () { let val = $inp.val(); if (val) { // $h1.text(val); $h1.html(val); $inp.val(function (index, oldValue) { // return oldValue.toUpperCase() return '' }) } }) </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jq樣式操做</title> <style> .box { /*width: 200px;*/ height: 200px; background-color: pink; } .sup-box { /*width: 400px;*/ height: 100px; background-color: orange; border-radius: 50%; line-height: 100px; text-align: center; color: red; } </style> </head> <body> <div class="box" style="width: 600px">文本</div> </body> <script src="js/jquery-3.4.1.min.js"></script> <script> let $box = $('.box'); $box.click(function () { // 獲取 let width = $box.css('width'); console.log(width); // 單個設置 $box.css('background-color', function (i, o) { console.log(o); return 'red' }); // 多條設置 $box.css({ width: '100px', height: '100px', backgroundColor: 'blue', }); if ($box.hasClass('sup-box')) { $box.removeClass('sup-box') } else { $box.addClass(function (i, o) { console.log(i, o); return 'sup-box' }) } }) </script> <script> // localStorage['name'] = 'owen'; // sessionStorage['age'] = 18; </script> </html>