語法: $('div').click(function () { 處理的事情 })javascript
$('div').click(function () { $(this).css('backgroundColor', 'red') }); $('div').mouseenter(function () { $(this).css('backgroundColor', 'black') })
由於普通註冊事件方法的不足,jQuery又建立了多個新的事件綁定方法bind() / live() / delegate() / on()等,其中最好用的是: on()css
$('div').on({ click: function () { pass}, mouseenter: function () { pass } })
$('div').on("mouseenter mouseleave", function () { $(this).toggleClass('current') })
$('ul').on('click', 'li', function () { alert('111') })
$('ol').on('click', 'li', function () { alert(222) }) var li = $('<li>lo的li</li>') $('ol').prepend(li)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { width: 100px; height: 100px; background-color: pink; } .current { background-color: purple; } </style> <script src="jquery.min.js"></script> </head> <body> <div></div> <ul> <li>咱們都是好孩子</li> <li>咱們都是好孩子</li> <li>咱們都是好孩子</li> <li>咱們都是好孩子</li> <li>咱們都是好孩子</li> </ul> <ol> </ol> <script> $(function () { // 1. 單個事件 $('div').click(function () { $(this).css('backgroundColor', 'red') }); $('div').mouseenter(function () { $(this).css('backgroundColor', 'black') }) $('div').on({ click: function () { pass }, mouseenter: function () { pass } }) // 2. 事件處理, 能夠綁定1個或者多個 $('div').on({ click: function () { $(this).css('backgroundColor', 'red') }, mouseenter: function () { $(this).css('backgroundColor', 'blue') }, mouseleave: function () { $(this).css('width', '300px') } }) // 2.1 若是處理程序相同 $('div').on("mouseenter mouseleave", function () { $(this).toggleClass('current') }) // 2.2 on能夠實現事件委託(委派) $('ul').on('click', 'li', function () { alert('111') }) // 2.3 on 能夠給爲來動態建立的的元素綁定事件 $('ol').on('click', 'li', function () { alert(222) }) var li = $('<li>lo的li</li>') $('ol').prepend(li) }) </script> </body> </html>
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> * { margin: 0; padding: 0 } ul { list-style: none } .box { width: 600px; margin: 100px auto; border: 1px solid #000; padding: 20px; } textarea { width: 450px; height: 160px; outline: none; resize: none; } ul { width: 450px; padding-left: 80px; } ul li { line-height: 25px; border-bottom: 1px dashed #cccccc; display: none; } input { float: right; } ul li a { float: right; } </style> <script src="jquery.min.js"></script> <script> $(function () { $('.btn').on('click', function () { var li = $('<li></li>') li.html($('.txt').val() + "<a href='javascript:;'> 刪除</a>") $("ul").prepend(li); li.slideDown(); // 下滑 $('.txt').val('') }) $('ul').on('click', 'a', function () { $(this).parent().slideUp(function () { // 上滑 $(this).remove(); }); }) }) </script> </head> <body> <div class="box" id="weibo"> <span>微博發佈</span> <textarea name="" class="txt" cols="30" rows="10"></textarea> <button class="btn">發佈</button> <ul> </ul> </div> </body> </html>
$('div').off()
$('div').off('click')
$('ul').off('click', 'li')
$('p').one('click', function () {
console.log('one');
})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { width: 100px; height: 100px; background-color: pink; } </style> <script src="jquery.min.js"></script> <script> $(function () { $('div').on({ click: function () { console.log('click'); }, mouseover: function () { console.log('mouseover'); } }) $('ul').on('click', 'li', function () { console.log('li'); }) // 1. off div身上的所有程序解綁 $('div').off() // 1.1 off 解除某一項事件 $('div').off('click') // 1.3 off 解除事件委託 $('ul').off('click', 'li') // 2. one() 可是它只能觸發事件一次 $('p').one('click', function () { console.log('one'); }) }) </script> </head> <body> <div></div> <ul> <li>咱們都是好孩子</li> <li>咱們都是好孩子</li> <li>咱們都是好孩子</li> </ul> <p>我是屁</p> </body> </html>
jQuery 爲咱們提供了兩個自動觸發事件 trigger() 和 triggerHandler() ; html
$('div').click()
$('div').trigger('click')
$("input").triggerHandler("focus");
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { width: 100px; height: 100px; background-color: pink; } </style> <script src="jquery.min.js"></script> <script> $(function () { $('div').on('click', function () { alert(111) }) // 自動觸發事件 // 1. 元素.事件() $('div').click() // 2. 元素.trigger("事件") // $('div').trigger('click') // $('input').trigger('focus') // 3. 元素.triggerHandler("事件") 就是不會觸發元素的默認行爲 // $('div').triggerHandler('click') // $("input").on("focus", function () { // $(this).val("你好嗎"); // }); $("input").triggerHandler("focus"); }) </script> </head> <body> <div></div> <input type="text"> </body> </html>
語法:java
e 就是事件對象jquery
$('div').on('click', function (e) {
pass
)}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> div { width: 100px; height: 100px; background-color: pink; } </style> <script src="jquery.min.js"></script> <script> $(function () { $(document).on('click', function () { console.log('點擊了document'); }) $('div').on('click', function (e) { console.log('點擊了DIV'); // console.log(e); e.stopPropagation() // 阻止事件冒泡 e.preventDefault() // 阻止默認行爲 }) }) </script> </head> <body> <div></div> </body> </html>
$.extend([deep], target, obj1, [objN])git
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="jquery.min.js"></script> <script> $(function () { // 1 // var targetObj = {}; // var obj = { // id: 1, // name: "andy" // }; // $.extend(targetObj, obj); // console.log(targetObj); // 2 // var targetObj = { // id: 0 // }; // var obj = { // id: 1, // name: "andy" // }; // $.extend(targetObj, obj); // // 會覆蓋targetObj 裏面原來的數據 // console.log(targetObj); // 3 var targetObj = { id: 0, msg: { sex: '男' } }; var obj = { id: 1, name: "andy", msg: { age: 18 } }; // 淺拷貝 $.extend(targetObj, obj); console.log(targetObj); // 會覆蓋targetObj 裏面原來的數據, 包括 msg 裏面的數據 msg: {age: 20} targetObj.msg.age = 20; // 淺拷貝添加 msg.age = 20 console.log(targetObj); // msg: {age: 20} console.log(obj); // 被拷貝對象也會改變 msg: {age: 20} // 深拷貝 $.extend(true, targetObj, obj); console.log(targetObj); // 徹底複製一份給目標對象 若是裏面有不衝突的屬性,會合併到一塊兒 targetObj.msg.age = 20; console.log(targetObj); // msg :{sex: "男", age: 20} console.log(obj); // {age: 18} 被拷貝對象不變 }) </script> </head> <body> </body> </html>
懶加載github
懶加載只需引入html 和 js操做 便可,此插件不涉及css。ide
<script src="js/EasyLazyload.min.js"></script> <script> lazyLoadInit({ showTime: 1100, onLoadBackEnd: function(i, e) { console.log("onLoadBackEnd:" + i); }, onLoadBackStart: function(i, e) { console.log("onLoadBackStart:" + i); } }); </script>
<img data-lazy-src="upload/floor-1-3.png" alt="">
相關資料: https://github.com/1515806183/html-code/tree/master/jQuery-03ui