当前位置:网站首页>自定义属性
自定义属性
2022-08-02 14:20:00 【铃儿响叮当不响】
1. 用户自己添加的属性
语法:元素.setAttribute('属性名',属性值)
2. 获取自定义属性值
语法:元素.getAttribute('属性名')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box"></div>
<script>
let box = document.querySelector('.box')
// 设置自定义属性
box.setAttribute('index',0)
console.log(box.getAttribute('index')) // 0
</script>
</body>
</html>
3. html5推出了专门的 data- 自定义属性 自定义属性一律以data-开头
获取该元素所有的自定义属性 语法:元素.dataset
得到该元素某个自定义属性值 语法: 元素.dataset.某个属性名
<body>
<div class="box" data-index="0" data-name="莉莉"></div>
<script>
// 设置自定义属性
let box = document.querySelector('.box')
console.log(box.dataset)
console.log(box.dataset.index) //0
</script>
</body>
边栏推荐
- 职工管理系统(SSM整合)
- DOM —— 事件机制及事件链
- 解决跨域的方法 --- Proxy
- velocity模板页面四则运算
- 网络运维系列:GoDaddy Shell DDNS配置
- 2021 Huawei Cup Mathematical Modeling Contest E question - Ultra-Wideband (UWB) precise positioning problem under signal interference
- Jenkins 参数化构建(Extended Choice Parameter)
- 【网络设备远程登陆配置SSH/Telnet 】
- synchronized详解
- lammps学习(一)单晶硅纳米磨削
猜你喜欢
随机推荐
加点字符就能让qq昵称很酷的神奇代码?
Nvm,Nrm使用教程
Mysql-Explain与索引详解
Based on the SVM regression forecast 】 【 LibSVM realize the prediction of a characteristic data
ks.cfg 怎么读取光盘 (cdrom) 上的文件并执行对应的脚本
golang中使用泛型
makefile——library
Redis6
Cookie 和 Session
VMware 安装openwrt
Golang学习(三十五) go 连接redis
golang八股文整理(持续搬运)
解决跨域问题的方法 --- CORS
DOM — 元素的增删改查
H3C 交换机配置端口组、DHCP、DHCP中继、管理用户
2021年华为杯数学建模竞赛E题——信号干扰下的超宽带(UWB)精确定位问题
【交换机端口安全技术 】
常见(MySQL)面试题(含答案)
数据源,分层开发以及jsp标签总结及相关代码
一分钟之内搭建自己的直播服务器?