当前位置:网站首页>自定义属性
自定义属性
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>

边栏推荐
猜你喜欢

JS中的数组方法和循环
【时间序列模型】AR模型(原理剖析+MATLAB代码)

DOM - Event Object

Mysql删库恢复数据

【频域分析】频谱泄露、频率分辨率、栅栏效应

为什么 RTP 的视频的采样率是 90kHz ?

这几年让你大呼惊人的AI应用,都离不开这项技术

Based on the SVM regression forecast 】 【 LibSVM realize the prediction of a characteristic data

在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法

VMware 安装openwrt
随机推荐
数据源,分层开发以及jsp标签总结及相关代码
web渗透之sql注入
DOM - Element Box Model
Spark的概念、特点、应用场景
VMware 安装openwrt
RTMP, RTSP, SRT 推流和拉流那些事
小知识点系列:StringUtil.isEmpty()与StringUtil.isBlank()的区别
CUDA programming based on Visual Studio 2015 (1): basic configuration
加点字符就能让qq昵称很酷的神奇代码?
C语言的基本程序结构详细讲解
Mysql理解MVCC与BufferPool缓存机制
【TCP 和 UDP 基本原理】
static关键字的三种重要作用详解
(三)文件操作之一——文件IO
【网络设备远程登陆配置SSH/Telnet 】
网络运维系列:端口占用、端口开启检测
个人成长系列:业务、技术学习书单
makefile——杂项
test2
2021 annual summary - complete a year of harvest