x
 
<!DOCTYPE html>
<html>
<body>
<h1>Document 对象</h1>
<h2>document.getElementsByName 方法</h2>
猫:
<input name="animal" type="checkbox" value="Cats">
狗:
<input name="animal" type="checkbox" value="Dogs">
<p>选中 name="animal" 的复选框:</p>
<script>
const collection = document.getElementsByName("animal");
for (let i = 0; i < collection.length; i++) {
  if (collection[i].type == "checkbox") {
    collection[i].checked = true;
  }
}
</script>
</body>
</html>