关于JavaScript的removeAttribute()移除属性怎么会移除最后一个?
本文介绍了关于JavaScript的removeAttribute()移除属性怎么会移除最后一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
<!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
}
解决方案
我在codepen上重现并且更正了你的代码,没有发现问题链接
这篇关于关于JavaScript的removeAttribute()移除属性怎么会移除最后一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文