Blogger Information
Blog 54
fans 4
comment 1
visits 54799
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取元素的属性值
神仙不在的博客
Original
1441 people have browsed it

1、利用element.属性:

2、element.getAttribute('属性')

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="box" data-index="1"></div>
<script>
    var box = document.getElementById('box');
    // 方法一:利用element.id,获取内置的属性值
    console.log(box.id);
    // 方法二;利用element.getAttribute('id');获取自定义属性:data-index='1',类比自带属性 id='color'
    console.log(box.getAttribute('id'));
    // 获取自定义属性:data-index='1',类比自带属性 id='color',只能用这个方法。
    console.log(box.getAttribute('data-index'));
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post