JS点击图片指定对象变色两种方法

编程入门 行业动态 更新时间:2024-10-25 02:30:01

JS点击图片指定对象变色<a href=https://www.elefans.com/category/jswz/34/1768716.html style=两种方法"/>

JS点击图片指定对象变色两种方法

要求:点击上面的颜色实现下面的图像变成相同的颜色

 

难点:对于js函数的this对象不太清楚如何传递

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>changeColor</title><style>td {width: 50px;height: 50px;}</style>
</head>
<body><table><tr><td style="background-color: #2f74ff"></td><td style="background-color: yellow"></td><td style="background-color: pink"></td><td style="background-color: red"></td><td style="background-color: green"></td><td style="background-color: grey"></td><td style="background-color: chocolate"></td></tr></table><table id="square" style="width: 500px; height: 500px; background-color: lightblue"></table><script>function changColor(){console.log(this)var bc = this.style.backgroundColorvar target = document.getElementById('square')target.style.backgroundColor = bc}var tot = document.querySelectorAll('table tr td')for (const obj of tot) {obj.onclick = changColor}</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>changeColor</title><style>td {width: 50px;height: 50px;}</style>
</head>
<body><table><tr><td style="background-color: #2f74ff" onclick="changColor(this)"></td><td style="background-color: yellow" onclick="changColor(this)"></td><td style="background-color: pink" onclick="changColor(this)"></td><td style="background-color: red" onclick="changColor(this)"></td><td style="background-color: green" onclick="changColor(this)"></td><td style="background-color: grey" onclick="changColor(this)"></td><td style="background-color: chocolate" onclick="changColor(this)"></td></tr></table><table id="square" style="width: 500px; height: 500px; background-color: lightblue"></table><script>function changColor(obj){console.log(obj)var bc = obj.style.backgroundColorvar target = document.getElementById('square')target.style.backgroundColor = bc}</script>
</body>
</html>

 

更多推荐

JS点击图片指定对象变色两种方法

本文发布于:2023-11-15 18:17:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种   对象   方法   图片   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!