当前位置:网站首页>【JS给元素添加属性:setAttribute;classList.remove;classList.add;】
【JS给元素添加属性:setAttribute;classList.remove;classList.add;】
2022-07-01 01:00:00 【Min_Fox】
- 首先获取当前元素
html
<img id="zi" class="zi" src="/src/image/w.png">
js
const zi = document.getElementById("zi");
- 修改img元素的src属性
使用setAttribute来设置属性。
js
zi.setAttribute("src", this.key);
- 给元素添加class
使用classList.remove来删除 class、classList.add来新增class,可以用来修改样式。
js
// 删除class
zi.classList.remove("update-file-block");
// 增加class
zi.classList.add("update-file-none");
css
.update-file-block {
display: inline-block;
}
.update-file-none {
display: none !important;
}
边栏推荐
- Mysql database foundation: process control
- KS009基于SSH实现宠物管理系统
- 图的连通性基础
- Handsontable data grid component
- 尝试新的可能
- Lecun, a Turing Award winner, pointed out that the future of AI lies in self-learning, and the company has embarked on the journey
- php将二维数组元素转为键值对
- Uniapp official component clicking item is invalid, solution
- [dynamic planning] path dp:931 Minimum Falling Path Sum
- What will Web3 bring in the future?
猜你喜欢
随机推荐
图灵奖得主LeCun指明AI未来的出路在于自主学习,这家公司已踏上征途
Mysql database foundation: process control
Digital IC design process summary
gin_gorm
Handsontable数据网格组件
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images
Service grid ASM year end summary: how do end users use the service grid?
The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
Introduction and principle analysis of cluster and LVS
[leetcode] climb stairs [70]
Looksrare team's "cash out" caused disturbance
Koa koa combine routes sub route management
[simulation] 922 Sort Array By Parity II
WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
日志 logrus第三方库的使用
1500w播放下还藏着什么热点?B站2个未来趋势你不得错过
微生物安全与健康,什么是生物处理?
Open3d point cloud bounding box
远程办公如何保持高效协同,实现项目稳定增长 |社区征文
45 year old programmer tells you: why do programmers want to change jobs? It's too true









