当前位置: 首页 > news >正文

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>

 

http://www.lryc.cn/news/223618.html

相关文章:

  • 什么是浏览器指纹?指纹浏览器如何避免浏览器指纹的追踪识别?
  • uni-app 、Spring Boot 、ant Design 打造的一款跨平台包含小说(仿真翻页、段落听书)、短视频、壁纸等功能含完备后台管理的移动应用
  • STM32-EXTI中断
  • 2023云栖大会,Salesforce终敲开中国CRM市场
  • 工业镜头接口类型
  • 生产环境中oracle dba权限检查和回收相关命令汇总
  • 实现微信转账到零钱经验
  • SpringCloud——负载均衡——OpenFeign
  • 力扣370周赛
  • EMNLP2023 | 让模型学会将提示插入到合适的中间层
  • 【PG】PostgreSQL单机部署(简洁命令版)
  • AI:69-基于深度学习的音乐推荐
  • php 使用phpoffice/phpspreadsheet拓展实现导出图片
  • 几种解决mfc140.dll文件缺失的方法,电脑提示mfc140.dll怎么办
  • 并发修改异常
  • split() 函数实现多条件转为数据为数组类型
  • 【Springboot】Vue3-Springboot引入JWT实现登录校验以及常见的错误解决方案
  • VueCli 自定义创建项目及配置
  • 2024年节假日sql脚本(区分休息日、节假日、工作日、调休工作)
  • vue3介绍
  • Spark SQL自定义collect_list分组排序
  • 2023年云计算的发展趋势如何?
  • uniapp中picker 获取时间组件如何把年月日改成年月日默认时分秒为00:00:00
  • k8s operator
  • 使用io_uring
  • LeetCode算法题解(回溯)|LeetCode93. 复原 IP 地址、LeetCode78. 子集、LeetCode90. 子集 II
  • vue、react数据绑定的区别?
  • 前端Vue 页面滑动监听 拿到滑动的坐标值
  • CSS实现鼠标移至图片上显示遮罩层及文字效果
  • 【OpenCV实现图像:图像处理技巧之空间滤波】