HI all!我有个疑问。我需要通过它的id获取元素。我知道我可以使用document.getElementById()或jquery选择器,但我不想使用jquery或任何其他库。这个想法是只使用js和没有库来构建一个组件。js通过编号获取元素
我得到这个情况下,通过代码生成的代码:
<div id="objprop">
<div id="prop-header"><span>Mi Ventana</span></div>
<div id="prop_width" style="clear: both;">
<label style="margin-left: 7px; margin-bottom: 7px; float: left;">Width</label>
<input type="text" style="float: right; margin-right: 7px; width: 70px;" id="width">
</div>
<div id="prop_height" style="clear: both;">
<label style="margin-left: 7px; margin-bottom: 7px; float: left;">Height</label>
<input type="text" style="float: right; margin-right: 7px; width: 70px;" id="height">
</div>
<div id="prop_left" style="clear: both;">
<label style="margin-left: 7px; margin-bottom: 7px; float: left;">Left</label>
<input type="text" style="float: right; margin-right: 7px; width: 70px;" id="left">
</div>
<div id="prop_top" style="clear: both;">
<label style="margin-left: 7px; margin-bottom: 7px; float: left;">Top</label>
<input type="text" style="float: right; margin-right: 7px; width: 70px;" id="top">
</div>
</div>
这样,让supose我想设置的输入宽度id的值。我不确定如何使用document.getElementById,因为可能是html中具有相同id的其他元素。
清楚,是我的疑问
我不清楚这是一个问题。你在问什么? DOM中是否有多个元素可能具有相同的ID? – Zach 2011-04-09 23:23:23
@Zach有可能,如果页面编码器不知道“ID”值必须是唯一的:-) – Pointy 2011-04-09 23:24:24
谢谢。那么我的错误;) – 2011-04-09 23:27:37