jQuery 未在 HTML 中找到所有预期的锚链接
我正在使用 jQueryPad 来尝试一些 JavaScript,但我陷入了困境。假设我有以下 HTML 片段:
<TABLE style="BORDER-COLLAPSE: collapse" id=phMain_Questions class=GridView border=1 rules=all cellSpacing=0><TBODY>
<TR class=GridViewHeader>
<TH scope=col> </TH>
<TH scope=col> </TH></TR>
<TR class=GridViewRow>
<TD><A id=hlQuestionDetails href="javascript:AddQuestionWindow_Open(3, 39, 'False');">This is a test question</A> </TD>
<TD><A href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$phMain$gvQuestions$ctl02$ctl00", "", true, "", "", false, true))'>Remove</A> </TD></TR>
<TR class="GridViewRow GridViewRowAlt">
<TD><A id=hlQuestionDetails href="javascript:AddQuestionWindow_Open(3, 40, 'False');">This is an option question.</A> </TD>
<TD><A href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$phMain$gvQuestions$ctl03$ctl00", "", true, "", "", false, true))'>Remove</A> </TD></TR></TBODY></TABLE>
我想要的是挑选出 id
属性为 hlQuestionDetails
的所有 a
标记。我的尝试是:
var links = $("a#hlQuestionDetails");
alert($(links).length);
找到的 a
标签数量为 1,但正如您在 HTML 中看到的,实际上有 2 个具有该名称的 a
标签。
有人能指出我在这里做错了什么吗,因为我不明白为什么 jQuery 会留下 a
标签之一。
编辑:
关于评论,我看到了问题。 HTML 由 ASP.NET gridview 控件生成。因此,我猜我应该使用类属性? 干杯。 贾斯。
I'm using jQueryPad to try out a bit of JavaScript and I'm stuck. Given I have the following HTML snippet:
<TABLE style="BORDER-COLLAPSE: collapse" id=phMain_Questions class=GridView border=1 rules=all cellSpacing=0><TBODY>
<TR class=GridViewHeader>
<TH scope=col> </TH>
<TH scope=col> </TH></TR>
<TR class=GridViewRow>
<TD><A id=hlQuestionDetails href="javascript:AddQuestionWindow_Open(3, 39, 'False');">This is a test question</A> </TD>
<TD><A href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$phMain$gvQuestions$ctl02$ctl00", "", true, "", "", false, true))'>Remove</A> </TD></TR>
<TR class="GridViewRow GridViewRowAlt">
<TD><A id=hlQuestionDetails href="javascript:AddQuestionWindow_Open(3, 40, 'False');">This is an option question.</A> </TD>
<TD><A href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$phMain$gvQuestions$ctl03$ctl00", "", true, "", "", false, true))'>Remove</A> </TD></TR></TBODY></TABLE>
What I want is to pick out all the a
tags whose id
attribute is hlQuestionDetails
. My stab at this is:
var links = $("a#hlQuestionDetails");
alert($(links).length);
The number of a
tags found is 1, but as you can see in the HTML, there are in fact 2 a
tags with that name.
Can someone point what I'm doing wrong here, as I can't figure out why jQuery is leaving one of the a
tags out.
EDIT:
In regards to the comments, I see the problem. The HTML was generated by an ASP.NET gridview control. So instead, I should use a class attribute I guess?
Cheers.
Jas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
属性 id 应该是元素的唯一标识符。您应该改用类属性。
Attribute id should unique identifier of element. You should use class attribute instead.
ID 在 HTML 中必须是唯一的,ID 选择器映射到
document.getElementById
,它返回具有该 ID 的第一个匹配元素 - 为每个元素提供一个class< /code> 相反,并使用类选择器:
IDs must be unique in HTML, and the ID selector maps to
document.getElementById
, which returns the first matching element with that ID - give each element aclass
instead, and use the class selector:ID 在 HTML 文档中必须是唯一的(因此ID)。如果有多个元素具有相同的 ID,大多数浏览器仅返回第一个元素。使用类来代替
并使用类选择器检索元素:
IDs have to be unique in an HTML document (hence ID). Most browsers only return the first element if there are several elements with the same ID. Use classes instead
and retrieve the elements with the class selector:
id 应该是 html 页面中的唯一标识符。这就是 getElementById(...) 仅返回 1 个元素的原因。我想 jQuery 也会做同样的事情。尝试使用 class 属性和选择器:
The id should be a unique identifier in a html page. That's why getElementById(...) returns only 1 element. I guess jQuery does the same. Try using a class attribute and selector instead:
标签的 ID 应该是唯一的,并且应该在引号内。如果您想定位多个标签,请使用虚拟类,即“Question”,然后使用
$('.Question')
来定位您的标签。The ID for a tag should be unique and it should be within quotes. If you want to target several tags then use a dummy class, i.e. "Question" and then use
$('.Question')
to target your tags.