如果有人能向我解释下面的代码在做什么,我将非常感激。作为JavaScript的新手,我只对它的功能有一个粗略的概念。这将是一个很大的帮助。如果问题太天真,也请原谅。以下javascript代码的说明
var init = function() {
var box = document.querySelector('#theArt').children[0],
showPanelButtons = document.querySelectorAll('#show-buttons input'),
panelClassName = 'show-front',
onButtonClick = function(event) {
box.removeClassName(panelClassName);
panelClassName = event.target.className;
box.addClassName(panelClassName);
};
for (var i = 0, len = showPanelButtons.length; i < len; i++) {
showPanelButtons[i].addEventListener('click', onButtonClick, false);
}
};
window.addEventListener('DOMContentLoaded', init, false);
这是HTML代码来实现
<div id="theArt">
<div id="cube" class="show-front">
<figure class="front"><img src="../_images/feature1.jpg" width="270px" height="270px"></figure>
<figure class="back"><img src="../_images/feature2.jpg" width="270px" height="270px"></figure>
<figure class="right"><img src="../_images/feature3.jpg" width="270px" height="270px"></figure>
<figure class="left"><img src="../_images/feature4.jpg" width="270px" height="270px"></figure>
<figure class="top"><img src="../_images/painting01.png" width="270px" height="270px"></figure>
<figure class="bottom"><img src="../_images/painting02.png" width="270px" height="270px"></figure>
</div>
<div id="options">
<p id="show-buttons">
<input type="image" src="../_images/feature1.jpg" width="90" height="90" class="show-front"></input>
<input type="image" src="../_images/feature2.jpg" width="90" height="90" class="show-back"></input>
<input type="image" src="../_images/feature3.jpg" width="90" height="90" class="show-right"></input>
<input type="image" src="../_images/feature4.jpg" width="90" height="90" class="show-left"></input>
<input type="image" src="../_images/painting01.png" width="90" height="90" class="show-top"></input>
<input type="image" src="../_images/painting02.png" width="90" height="90" class="show-bottom"></input>
</p>
['DOMContentLoaded'](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded),['addEventListener']( https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener),['querySelectorAll'](https://developer.mozilla.org/en-US/docs/Web/API/document/querySelectorAll),['querySelector'](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector),['for'](https://developer.mozilla .org/en/docs/Web/JavaScript/Reference/Statements/for) – Rayon
@RayonDabre我在网上查过,它是一个javascipt dom,从元素中删除一个类...我想在这里它从类“panelClassName”中删除类班级.....有更多的关于我n此链接http://api.prototypejs.org/dom/Element/removeClassName/ –
@RayonDabre是的代码工作正常..我用它与modernizer ..我对代码的理解是:var box被分配第一个孩子id为“theArt”,showPanelButton都是ID“show-buttons”中的输入ellement,panelClassName是类“show-front”。 idont了解onButtonClick函数。 –