当前位置:网站首页>【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;
}
边栏推荐
猜你喜欢

孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久

New opportunities for vr/ar brought by metauniverse

3dsmax plug-in development traversal node object and object acquisition and inode transformation matrix description

未来的 Web3会带来什么?

视频教程 | 长安链推出系列视频教程合集(入门)

Uniapp official component clicking item is invalid, solution

flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘

远程办公如何保持高效协同,实现项目稳定增长 |社区征文

PHP crawls data through third-party plug-ins

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"?
随机推荐
Uniapp official component clicking item is invalid, solution
二季度最后一天
未来的 Web3会带来什么?
Some items of OCR
[Office PDF] PDF merging and splitting will free us from the functional limitations of paid software, OK
[leetcode] sum of two numbers [1]
Open3d point cloud color rendering
mysql插入\更新前+判断条件
Ks009 implementation of pet management system based on SSH
数字IC设计流程总结
laravel+redis 生成订单号-当天从1开始自增
【栈】921. Minimum Add to Make Parentheses Valid
微生物安全与健康,什么是生物处理?
PHP converts two-dimensional array elements into key value pairs
MFC TCP communication server client demo notes vs2019
[deepin] common sets
做生意更加务实
短视频平台开发,依靠DrawerLayout实现侧滑菜单效果
Mysql database foundation: process control
More pragmatic in business