JavaScript数组ID [英] Javascript Array ID's

查看:235
本文介绍了JavaScript数组ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含我需要使用JQuery访问的元素的HTML页面。这是一个如何识别表单元素的例子:

 < div id =nItemIDLineclass = formLine> 
< span id =nItemID [766] Labelclass =formLabel alignTop>电话:< / span>
< / div>

当我尝试显示特定元素的ID时,它告诉我:nItemID [766] Label , 例如。但是,当我输入代码时,它找不到任何东西......

  alert($(#nItemID [766] Field)。val()); 

有人可以帮助我访问这些特定元素吗?



谢谢!

解决方案

您必须用反斜杠将方括号转义:


$ b $

  alert($('#nItemID \\ [766\\] Field').val()); 

有这样的id值有点问题,但我一直处于情况他们很难避免。



编辑—请注意,您需要在字符串中使用两个反斜杠,以便jQuery选择器解释器可以看到它们;也就是说,你需要在字符串中留下一个反斜杠,而在JavaScript中这样做的方式是将反斜杠加倍我讨厌解释



再次编辑—以下是HTML 5草案中关于id属性的说明:


3.2.3.1 id属性

id属性指定其元素的唯一标识符(ID)。 [DOMCORE]

该元素的主子树中的所有ID必须是唯一的,并且必须至少包含一个字符。该值不能包含任何空格字符。



元素的唯一标识符可用于各种用途,最显着的是作为链接到文档特定部分的一种方式使用片段标识符,作为在脚本编写时定位元素的一种方式,以及从CSS定义特定元素的方式。

标识符是不透明的字符串。特定的含义不应该从id属性的值中派生出来。


没有关于id属性值的规则,在其他单词,除了它们不能包含空格。


I've got a HTML page containing elements that I need to access using JQuery. Here is an example of how the form elements are identified:

<div id="nItemID[766]Line" class="formLine ">
   <span id="nItemID[766]Label" class="formLabel alignTop">Téléphone:</span>
   <input id="nItemID[766]Field" class="formField" type="text" maxlength="11" value="" name="nItemID[766]" style="width: 300;">
</div>

When I try to show the ID of a particular element, it tells me: nItemID[766]Label, for example. But when I type that code, it doesn't find anything...

alert( $("#nItemID[766]Field").val() );

Could someone help me with the way to access those particular elements ?

Thank you!

解决方案

You'll have to escape the square brackets with backslashes:

alert($('#nItemID\\[766\\]Field').val());

It's a little bit questionable to have "id" values like that, but I've been in situations in which they're hard to avoid.

edit — note that you need two backslashes in the string so that the jQuery selector interpreter can "see" them; that is, you need to leave a single backslash in the string, and the way to do that in JavaScript is to double the backslash I hate explaining that.

edit again — Here's what the HTML 5 draft has to say about "id" attributes:

3.2.3.1 The id attribute

The id attribute specifies its element's unique identifier (ID). [DOMCORE]

The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

An element's unique identifier can be used for a variety of purposes, most notably as a way to link to specific parts of a document using fragment identifiers, as a way to target an element when scripting, and as a way to style a specific element from CSS.

Identifiers are opaque strings. Particular meanings should not be derived from the value of the id attribute.

No rules about the values of "id" attributes, in other words, other than that they cannot contain spaces.

这篇关于JavaScript数组ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆