<!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 itemif (firstItem.hasAttribute('class')) { // If it has a class attribute firstItem.removeAttribute('class'); // Remove its class attribute}
關于JavaScript的removeAttribute()移除屬性怎么會移除最后一個?
慕無忌1623718
2018-12-19 10:19:09
