1

请帮帮我 !任务:使用纯js,混合图像(div)并使它们可点击:点击时,如果设置了一些背景属性,它应该删除图像,并在s clicked and there is no background at all (kinda toggling, but remember: the task was using the pure js knowing nothing about any frameworks). The problem is: there is 16 divs i get by getElementsByTagName (proven by alert :), and i can点击最后一个div时取回图像,因为当我写在最后一个 for 循环中,z<div_arr.length萤火虫抛出错误:div_arr[z]: undefined. 但是当它是z < div_arr.length - 1s okay, but last div isn点击的。

这是一段代码:

var div_arr=document.getElementById('wrapper').getElementsByTagName('div');     

// 随机排序图像数组

        document.getElementById('myButton').onclick = function (){
                for(var j=0;j < div_arr.length; j++){
                var randNum = Math.floor(Math.random() * div_arr.length); // число от 0 до 15
                tmp=div_arr[randNum].className;
                div_arr[randNum].className=div_arr[j].className;
                div_arr[j].className=tmp;
            };

// 事件处理:

                for(var z=0; z< div_arr.length-1;z++){
                div_arr[z].onclick=function(){
                if(this.style.background==false)
                              this.style.background='none';
                else
                   this.style.background=div_arr[z].style.background;

            };
4

0 回答 0