<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="cc" someAttr='itany'>cc</div>
<table>
<tr>
<td id="td" colspan="2">ccc</td>
</tr>
</table>
<script>
var cc = document.getElementById('cc');
console.log( cc.someAttr ); // undefined
console.log( cc.getAttribute('someAttr') ); // itany
console.log( document.getElementById('cc').colspan ); // undefined
// 解析:
// 1. dom对象不能直接取自定义的属性值
// 2. getAttribute() 方法返回指定属性名的属性值。
// 3. dom操作不能获取html的属性值
</script>
</body>
</html>
一个关于dom操作属性的面试题
最新推荐文章于 2024-04-27 07:28:37 发布