gpt4 book ai didi

javascript - js通过id获取元素

转载 作者:行者123 更新时间:2023-11-28 12:47:52 30 4
gpt4 key购买 nike

大家好!我有个疑问。我需要通过元素的 id 获取它。我知道我可以使用 document.getElementById() 或 jquery 选择器,但我不想使用 jquery 或任何其他库。这个想法是只使用 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>

所以,假设我想设置输入宽度 id 的值。我不确定是否使用 document.getElementById 因为 html 中可能有其他具有相同 id 的元素。

这是我的疑问

最佳答案

HTML specification规定 id 只能使用一次。它在整个文档中必须是唯一的。如果您正在处理有效文档,您应该可以轻松使用 document.getElementById()

关于javascript - js通过id获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5608599/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com