var hobby = document.getElementsByTagName("input"); for(i = 0;i < hobby.length;i++){ if(hobby[i].type == "checkbox"){ hobby[i].checked = true; } } } function clearall(){ var hobby = document.getElementsByName("hobby"); for(i = 0;i < hobby.length;i++){ hobby[i].checked = false;} }
i是for循环内的局部变量 ;
hobby 是项目内元素的name