简体   繁体   English

js通过id获取元素

[英]js get element by id

HI all! 大家好! I have a doubt. 我有个疑问。 I need to get an element via its id. 我需要通过其ID获取元素。 I know i can use document.getElementById() or jquery selector, but i don´t want to use jquery or any other library. 我知道我可以使用document.getElementById()或jquery选择器,但我不想使用jquery或任何其他库。 The idea is to build a component using just js and no libraries. 这个想法是只使用js而不使用库来构建组件。

I got this situation, code generated by code: 我遇到这种情况,代码是由代码生成的:

<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>

so, let supose i want to set the value of input width id with. 因此,让我想设置输入宽度id的值。 I´m not sure about using document.getElementById because may be other elements with same id in the html. 我不确定要使用document.getElementById,因为可能是html中具有相同ID的其他元素。

well that is my doubt 那是我的疑问

The HTML specification states that an id must only be used once. HTML规范指出, id只能使用一次。 It must be unique through-out a document. 在整个文档中,它必须是唯一的。 If you're dealing with valid documents, you should feel comfortable using document.getElementById() 如果您要处理有效的文档,则应该使用document.getElementById()

According to the specifications the id attribute of an element must be unique in a document. 根据规范 ,元素的id属性在文档中必须是唯一的。 So as long as the document conforms to the specs you should have no problem using getElementById(). 因此,只要文档符合规范,使用getElementById()就不会有问题。

The values of "id" attributes must be unique throughout the page. “ id”属性的值在整个页面中必须是唯一的。 You're right that it would be a problem for "getElementById()" to have to deal with the same "id" value being used on multiple elements; 没错,“ getElementById()”必须处理在多个元素上使用的相同“ id”值将是一个问题。 that's why you absolutely should not do that. 这就是为什么您绝对不应该这样做。 That's why it's called an "id" — it is the identifier for the element. 这就是为什么它被称为“ id”的原因-它是元素的标识符

I´m not sure about using document.getElementById because may be other elements with same id in the html. 我不确定要使用document.getElementById,因为可能是html中具有相同ID的其他元素。

Id MUST be unique. ID必须是唯一的。

http://validator.w3.org/ http://validator.w3.org/

thanks. 谢谢。

To solve my problem. 解决我的问题。 i did the follow: 我做了以下事情:

I create elements dynamic so i added a post fix like this: this.postfix = new Date().getTime(); 我创建动态元素,所以我添加了一个后缀,如下所示:this.postfix = new Date()。getTime();

so, when i have to add and input with id 'width' i add the postfix like it is named 'width_'+postfix. 因此,当我必须添加并输入ID为“宽度”的ID时,我会添加后缀,就像它被命名为“ width _” + postfix。 so i can use in a secure way document.getElementById; 这样我就可以安全地使用document.getElementById;

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM