当前位置:网站首页>三个点语法和DOM观察者
三个点语法和DOM观察者
2022-08-08 11:53:00 【勇敢*牛牛】
三个点语法和DOM观察者
var arr=[1,2,3,4];
var arr1=[...arr];
console.log(arr1)// [1, 2, 3, 4]
var arr=[1,2,3,4];
var arr1=[0,...arr,5,6];
console.log(arr1)//[0, 1, 2, 3, 4, 5, 6]
…arr 是剩余的参数
function fn(a,b,...arg){
console.log(a,b,arg)
}
fn(1,2,3,4,5,6);1 2 (4) [3, 4, 5, 6]
扩展对象键值对
var o={
a:1,b:2,c:3};
var o1={
e:5,...o};
console.log(o1)//{e: 5, a: 1, b: 2, c: 3}
复制一个对象
Object.assign({
},{
a:1,b:2});
DOM观察者
new MutationObserver(回调函数)
回调函数 有两个参数,一个mutationList观察变化的列表 observer 观察者
var div=document.querySelector("div");
var observer= new MutationObserver(function(mutationList,observer){
console.log(mutationList)
for(var i=0;i<mutationList.length;i++){
console.log(mutationList[i].oldValue)
}
// attributeName: "cd" 修改的标签属性
// type: "attributes" 变化的类型 attributes 标签属性变化 childList 子元素列表变化
// addedNodes: 增加的元素列表
// removedNodes 删除的元素列表
// target: span 目标元素
// oldValue: null 原来的值
})
根据上面创建的观察者实现观察,观察div的变化
observer.observe(div,{
attributes:true,//标签属性
childList:true,//子元素列表
subtree:true//子树
})
div.setAttribute("ab","3");
div.firstElementChild.setAttribute("cd","3")
div.firstElementChild.textContent="abc"
var span=document.createElement("span");
div.firstElementChild.appendChild(span);
span.remove();
div.lastElementChild.value="10"
div.lastElementChild.setAttribute("value","10")
observer.disconnect(); 停止观察
边栏推荐
猜你喜欢
随机推荐
微服务负载均衡器Ribbon实战
GC explanation and tuning of JVM
一文读懂配置管理(CM)
写个 shell 玩 数字炸弹
C# 反射 操作列表类型属性
模式识别 学习笔记:第六章 其他分类方法 (持续更新中。。。)
【访谈】Eotalk Vol.01:Eoapi,我们希望以开源的方式构建 API 生态系统
转转商品系统高并发实战(数据篇)
十年架构五年生活-08 第一次背锅
neural network classification
学习与尝试 --&gt; 事件风暴
四、哈希表
安科瑞预付费水电集团物业解决方案-Susie 周
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
【地平线旭日X3派试用体验】WIFI连接,SSH登录,TogetherROS安装(第二节)
(原创)[C#] GDI+ 之鼠标交互:原理、示例、一步步深入、性能优化
为你的网站加上live2d的动态小挂件,博君一晒
5S软件就是将软件应用全维度简单化的软件系统
如何上线TB级推荐模型
8/7 牛客6+div2D+倍增lca