请问老师,有一个不解的地方。
来源:7-12 JS对表单元素进行设置7
qq_慕妹9276277
2022-05-26 20:49:32
document.getElementsById
document.getElementById
俩个代码区别多了个s,有什么区别吗?代码里多个s的就失效了
function checkInterest(){ var interest=document.getElementsByName("interest"); for(i=0;i<interest.length;i++){ interest[i].checked=flag; } if(flag){ document.getElementsById("btn1").value="123"; document.getElementById("btn1").value="123"; }else{ document.getElementsById("btn1").value="456"; } /*第一轮全选必定是true,走完后变成false,再次点击就变成全不选了 */ flag=!flag;//开关变量 }
1回答
好帮手慕小脸
2022-05-27
同学你好,并且没有getElementsById这样一个方法,只有getElementById这个方法;这也说明了id是唯一的不能获取多个。getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用。如果需要查找文档中的一个特定的元素,最有效的方法是 getElementById(),在操作文档的一个特定的元素时,最好给该元素一个 id 属性,为它指定一个唯一的名称,然后就可以用该 ID 查找想要的元素。
祝学习愉快~
相似问题