未捕获类型错误:无法设置未定义的属性(设置"颜色")



document.querySelectorAll('button').forEach (function(button){
button.onclick=function(){
document.querySelector('#hello').Style.color=button.dataset.color;
}
});
<!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>
<h1 id="hello">Hello wprld</h1>
<button data-color="red">Red</button>
<button data-color="green">green</button>
<button data-color="blue">Blue</button>
</body>
</html>

//尝试在用户单击标签时更改标签的颜色//试图实现此功能,但它显示了相同的错误样式,不能为空

它区分大小写。不是";"样式";,它是";风格";。当它说未定义时,检查它是否存在

document.querySelectorAll('button').forEach (function(button){
button.onclick=function(){
document.querySelector('#hello').style.color=button.dataset.color;
}
});
<!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>
<h1 id="hello">Hello wprld</h1>
<button data-color="red">Red</button>
<button data-color="green">green</button>
<button data-color="blue">Blue</button>
</body>
</html>

相关内容

最新更新