当前位置:网站首页>使用js实现点击事件
使用js实现点击事件
2022-07-21 15:41:00 【51CTO】
这个项目实现一个页面上的3*3的格子,点击每个格子,改变其背景颜色。
效果如下:
1.首先在HTML文件的<body>标签中创建一个表格,背景颜色是红色:
①使用css文件:
首先在WebRoot的文件夹下创建一个CSS文件夹,创建一个index.css文件
写入如下代码:
td前面什么都没有,没有#,也没有.
表示只要是引用这个css文件的页面里的所有的td的属性都会把这些属性当作外部属性来调用
所以,在HTML文件的<body>标签下只需要写下如下代码:
效果如下:
2.点击没有反应,这时候需要写点击事件了
在这里,我们需要用到window.onload事件:
所以,我们让页面加载完成后,使用js来为我们的每个单元格添加一个点击事件处理方法:
这里,事件处理方法中什么都没有操作,现在我们只是能够获得我们点击的单元格的id属性:
我们添加一个整型的变量来存储我们点击的次数,以此来改变颜色:
然后添加对应的改变颜色的操作:
原始颜色为红色点击第一次背景色变为funchsia->blue->yellow->green->purple->red;
最终结果:
边栏推荐
- Keras深度学习实战(14)——从零开始实现R-CNN目标检测
- Template implementation of linked list
- ECCV 2022 | fix the performance damage of large targets caused by FPN: you should look at all objects
- side effect of intrinsics
- Matlab GUI programming skills (VIII): uitoolbar create toolbar in the drawing window
- dedecms编辑器支持Word图片自动粘贴
- CodeSys中编程实现串口通讯【基于树莓派4B】
- Scala函数及其高阶应用
- kettle入门杂乱笔记
- JMeter installation and setting (I)
猜你喜欢
Keras深度学习实战(13)——目标检测基础详解
谁是预制菜赛道的“卷”王?
第一个微信小程序的初始化过程、小程序微信开发平台的下载、如何注册一个微信小程序的账号
加密崩盘,Web3已死?
Oracle about date field index usage test
Keras深度学习实战(11)——可视化神经网络中间层输出
Keras deep learning practice (15) -- realize Yolo target detection from scratch
测试人员面试时常见问题
Keras' deep learning practice -- gender classification based on inception V3
dedecms编辑器支持Word图片自动粘贴
随机推荐
Matlab GUI programming skills (IX): explain the uitable function display table and related operations in detail (create table user interface)
可视化之基础设置
Keras深度学习实战(15)——从零开始实现YOLO目标检测
Awvs installation and problem solving
Airtest stepped on the pit -- start flash back
Idea SSH tool remote link failed
side effect of intrinsics
Isomorphism + cross end, knowing applet +kbone+ applet container is enough!
第一个微信小程序的初始化过程、小程序微信开发平台的下载、如何注册一个微信小程序的账号
Stack implementation of linked list structure
C#中抽象类abstract和接口interface的区别
Common commands for starting services
Interview algorithm questions
287寻找重复数
Druid 集成 ShardingSphere 出现 xxMapper.xml 报错的原因与解决方案
Dangling pointer and orphan memory
ECCV 2022 | fix the performance damage of large targets caused by FPN: you should look at all objects
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
【写实体类中,为何int要用Integer类型而不用int的原因】
How to add a map to a page