<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>无标题文档</title></head> <body><form>请选择你爱好:<br><input type="checkbox" name="hobby" id="hobby1"> 音乐<input type="checkbox" name="hobby" id="hobby2"> 登山<input type="checkbox" name="hobby" id="hobby3"> 游泳<input type="checkbox" name="hobby" id="hobby4"> 阅读<input type="checkbox" name="hobby" id="hobby5"> 打球<input type="checkbox" name="hobby" id="hobby6"> 跑步 <br><input type="button" value = "全选" onclick = "checkall();"></form><script type="text/javascript">function checkall(){var hobby = document.getElementsByTagName("input");hobby.forEach(function(v){v.checked=true;}); //为什么chrome这句报函数forEach不存在?[1,2,3].forEach(function(v){alert(v);});//而这句就不报错呢?}</script></body></html>
17 回答
杨少峰
TA贡献5条经验 获得超3个赞
这样吧:
// 模拟Array.forEach
Array.prototype.ForEach = function ( func ) {
for ( let i = 0; i<this.length; i++ ) {
func(this[i],i);
}
}
慕婉清8071797
TA贡献1条经验 获得超0个赞
添加回答
举报
0/150
提交
取消