





<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>01-事件处理on</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>1</li> <li>2</li> <li>3</li> </ul> <ol> </ol> <script> $(function () { // 1. 单个事件注册 // $("div").click(function () { // $(this).css("backgroundColor", "purple"); // }) // $("div").mouseenter(function () { // $(this).css("backgroundColor", "blue"); // }) // 2. 事件处理on // (1) 可以多个事件 一起绑定到同一个元素 // $("div").on({ // mouseenter: function () { // $(this).css("backgroundColor", "blue"); // }, // click: function () { // $(this).css("backgroundColor", "purple"); // }, // mouseleave: function () { // $(this).css("background", "skyblue"); // } // }) // 注意事件中间 用空格隔开 // $("div").on("mouseenter mouseleave", function () { // $(this).toggleClass("current"); // }) // (2) on 可以实现事件委托 (委派) // click绑定在ul身上 触发对象是小li $("ul").on("click", "li", function () { console.log($(this), $(this).text()); //$(this) 依次指向每个小li alert(11); }) // (3) on可以给未来动态创建的元素 绑定事件 // $("ol li").click(function () { //这种方式不可以给未来动态创建的元素 绑定事件 // alert(11); // }) $("ol").on("click", "li", function () { //这样可以给未来元素 绑定事件 alert(11); }) var li = $("<li>我是后来创建的</li>"); $("ol").append(li); }) </script></body></html>










<!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 () { // var targetObj = {}; // var obj = { // id: 1, // name: "andy" // }; // // $.extend(target, obj); // $.extend(targetObj, obj); // console.log(targetObj); // var targetObj = { // id: 0 // }; // var obj = { // id: 1, // name: "andy" // }; // // $.extend(target, obj); // $.extend(targetObj, obj); // console.log(targetObj); // 会覆盖targetObj 里面原来的数据 var targetObj = { id: 0, msg: { sex: '男' } }; var obj = { id: 1, name: "andy", msg: { age: 18 } }; // // $.extend(target, obj); // $.extend(targetObj, obj); // console.log(targetObj); // 会覆盖targetObj 里面原来的数据 // // 1. 浅拷贝把原来对象里面的复杂数据类型地址拷贝给目标对象 // targetObj.msg.age = 20; // console.log(targetObj); // console.log(obj); // age = 20 // 2. 深拷贝把里面的数据完全复制一份给目标对象 如果里面有不冲突的属性,会合并到一起 $.extend(true, targetObj, obj); // console.log(targetObj); // 会覆盖targetObj 里面原来的数据 targetObj.msg.age = 20; console.log(targetObj); // msg :{sex: "男", age: 20} console.log(obj); }) </script></head><body></body></html>






