@[TOC](jquery中的扩展方法 . e x t e n d ( ) 和 .extend() 和 .extend()和.fn.extend())
$.extend()
$.extend()函数用于将一个或多个对象的内容合并到目标对象
<script src="./jquery-1.12.4.js"></script>
<script>
let obj1 = {
name:'zhangsan',
age:18,
study:{
k:'大数据',
cj:'80'
}
}
let obj2 = {
name:'lisi',
car:'bmw',
biao:'lls',
study:{
k:'前端'
}
}
//将obj2合并到obj1中,
//deep:默认是false,浅拷贝:如果有重复的属性,后面会覆盖前面,但不会把cj继承下来;
// let newobjf = $.extend(obj1,obj2);
//console.log('false',newobjf)
//deep:true,深拷贝:如果有重复的属性,后面会覆盖前面,会把cj继承下来;
//显示输出合并的数据
let newobjt = $.extend(true,obj1,obj2);
console.log('true',newobjt)
</script>
效果:
$.fn.extend
<input type="checkbox" name="" id="">篮球
<input type="checkbox" name="" id="">足球
<input type="checkbox" name="" id="">电竞
<button onclick="allS()">全选</button>
<button onclick="noS()">取消全选</button>
<!-- 因为我们写的插件是基于jq的,所以要在jq引入之后,再引入 -->
<script src="./$.fn.extend扩展.js"></script>
<script>
/* $('input').setvalue(); */
/* $('input').setplacehold(); */
function allS(){
$('input[type=checkbox]').checkAll();
}
function noS(){
$('input[type=checkbox]').noAll();
}
</script>
$.fn.extend({
checkAll:function(){
/* 这上面的this代表的是调用者(input[type=checkbox]元素集合) */
this.each(function(index,item){
/* console.log(index,item) */
/* 这里的item是原生的元素 */
/* item.checked=true; */
/* $(item).prop('checked',true) */
/* attr 适用于自定义的属性 */
/* 对于元素本身存在的属性应该使用prop */
console.log(this);
/* 应为each里面的this是代表元素集合里面的每一个元素 */
this.checked = true;
})
},
noAll:function(){
this.each(function(index,item){
/* item.checked=false; */
/* $(item).prop('checked',false) */
this.checked = false;
})
}
})