// 示例代码:操作 HTML 元素的属性
// 获取元素
const element = document.getElementById('myElement');
// 设置属性
element.setAttribute('data-custom', 'customValue'); // 设置自定义属性
element.setAttribute('class', 'newClass'); // 设置类名
element.setAttribute('id', 'newId'); // 设置 ID
// 获取属性
const customAttributeValue = element.getAttribute('data-custom'); // 获取自定义属性值
console.log(customAttributeValue); // 输出: customValue
const classAttributeValue = element.getAttribute('class'); // 获取类名
console.log(classAttributeValue); // 输出: newClass
// 移除属性
element.removeAttribute('data-custom'); // 移除自定义属性
// 解释说明:
// 1. setAttribute 方法用于设置指定的属性值。
// 2. getAttribute 方法用于获取指定的属性值。
// 3. removeAttribute 方法用于移除指定的属性。
下一篇:js scrollleft
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站