我有这样的代码:
<form name="selectForm">
<select multiple name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select><br />
<input type="button" value="Select Cars" onclick="alert('You selected ' + howMany(document.selectForm.cars) + ' cars')" />
</form>
我的主要问题是关于这一行:document.selectForm.cars
它有什么作用?我在哪里可以找到相关信息来阅读它?为什么不使用通常的getElementBy...
? Mozilla只给出与此无关的接口(interface)。
最佳答案
您不太可能在最近的教程中看到这种模式,因为它有点过时:
document.selectForm
指的是form
带有 name
的元素(或者可能 id
) selectForm
的属性。 document.selectForm.cars
是<select>
其中名为 cars
的元素。这种旧的使用方式document.*name*
引用页面上的元素应替换为 document.getElementsByName
或(在为表格提供 ID 后)document.getElementById
在现代代码中。
您可以通过转到控制台并输入 document.selectForm
来解决这个问题,您会看到显示的元素
关于javascript - JS DOM - document.getElementById 与按名称选择之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28934916/