<!DOCTYPE html>
<html>
<head>
<title>JavaScript & jQuery - Chapter 5: Document Object Model - Remove Attribute</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/c05.css">
</head>
<body>
<div id="page">
<h1 id="header">List</h1>
<h2>Buy groceries</h2>
<ul>
<li id="one" class="hot"><em>fresh</em> figs</li>
<li id="two" class="hot">pine nuts</li>
<li id="three" class="hot">honey</li>
<li id="four">balsamic vinegar</li>
</ul>
</div>
<script src="js/remove-attribute.js"></script>
</body>
</html>
以上为HTML代码,以下为JavaScript代码。
书上说“这段脚本检查了选中元素是否包含class属性,如果包含,就把它移除掉。”按我的理解应该移除第一个啊,怎么会移除最后一个呢?是我理解的不对?
var firstItem = document.getElementById('one'); // Get the first item
if (firstItem.hasAttribute('class')) { // If it has a class attribute
firstItem.removeAttribute('class'); // Remove its class attribute
}
幕布斯7119047
相关分类