对象改成Map
来源:3-9 Map的应用
xcn_aaaa
2022-10-13 19:50:25
<!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>Document</title>
</head>
<body>
<p id = "p1">123</p>
<p id = "p2">123</p>
<p id = "p3">123</p>
<script>
// const p1 = document.getElementById('p1');
// const p2 = document.getElementById('p2');
// const p3 = document.getElementById('p3');
const [p1,p2,p3] = document.querySelectorAll('p');
let m = new Map([[p1,new Map([['color','red'],['backgroundColor','yellow'],['fontSize','40px']])],[p2,new Map([['color','green'],['backgroundColor','pink'],['fontSize','40px']])],[p3,new Map([['color','blue'],['backgroundColor','orange'],['fontSize','40px']])]])
console.log(m);
m.forEach((propMap,elem)=>{
propMap.forEach((value,key)=>{
console.log(value);
elem.style[key] = value;
})
})
</script>
</body>
</html>1回答
同学你好,代码是正确的。继续加油,祝学习愉快!