<!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>
* {
padding:30px;
}
</style>
</head>
<body>
<div id="oo">
<!-- <div><i>语</i><span></span></div>
<div><i>语</i><span></span></div>
<div><i>语</i><span></span></div> -->
</div>
<script>
var kemu = [
{ke:'数',name:'数学'},
{ke:'语',name:'语文'},
{ke:'英',name:'英语'}
]
new function () {
kemu.forEach(function (item,index) {
var div = document.createElement('div');
div.innerHTML += `<i>${item.ke}</i><span>${item.name}</span>`;
oo.appendChild(div);
function changeColor (x,y,z) {
switch (index) {
case 0 :
(x.childNodes[0].style.color = 'red',
x.childNodes[1].style.backgroundColor = 'red'); break;
case 1 :
(y.childNodes[0].style.color = 'blue',
y.childNodes[1].style.backgroundColor = 'blue'); break;
case 2 :
(z.childNodes[0].style.color = 'green',
z.childNodes[1].style.backgroundColor = 'green'); break;
}
}
changeColor(oo.childNodes[3],oo.childNodes[4],oo.childNodes[5]);
});
}
</script>
</body>
</html>
复制代码
js动态创建元素,并控制元素样式
最新推荐文章于 2025-04-03 12:43:45 发布