我正在尝试创建一个用户可以在那里创建自己的社交网络按钮的网站。(我知道它已经完成,但主要是为了练习)。该网站的一部分将允许用户选择按钮的形状。这是HTML:
<div class="design" id="shape">
<div class="shapeSelect square" id="square"></div>
<div class="shapeSelect rounded" id="rounded"></div>
<div class="shapeSelect circle" id="circle"></div>
</div>
我想做的是在单击 div 时添加一个事件侦听器。单击后,类属性将更改为“已选择”。当另一个将被单击时,第一个单击的将被清除并选择下一个。就像单选按钮一样。
我熟悉 JavaScript,我的想法是这样的:
window.onload = function () {
'use strict';
document.getElementById("square").addEventListener('click', function (e) {//adds the event listener
divArray = document.getElementById("shape");//Here is my first issue: an array is not returned
if (!(document.getElementById("square").getAttribute("class") == "shapeSelect square selected")) {// checks to make sure its not already selected
for (i = 0, count = document.getElementById("shape").length; i < count; i++) {// if it isn't go through the array
divArray[i]// and this is where i also get stuck. I Can't figure out how i would return the class attribute to be class="shapeSelect circle" instead of class="shapeSelect circle selected"
};
}
}, false);
}