当id包含一个点时,如何使用jquery通过ID选择html节点? [英] How to select html nodes by ID with jquery when the id contains a dot?
问题描述
如果我的 html 看起来像这样:
If my html looked like this:
<td class="controlCell">
<input class="inputText" id="SearchBag.CompanyName" name="SearchBag.CompanyName" type="text" value="" />
</td>
如何使用 JQuery 选择 #SearchBag.CompanyName?我无法让它工作,我担心是点破坏了这一切.烦人的是,重命名我所有的 id 需要做很多工作,更不用说可读性的损失了.
How could I select #SearchBag.CompanyName with JQuery? I can't get it to work and I fear it's the dot that's breaking it all. The annoying thing is that renaming all my id's would be a lot of work, not to mention the loss in readability.
注意:
请让我们不要开始谈论表格不是为布局而制作的.我非常了解 CSS 的价值和缺点 并尽可能地尝试使用它.
Note:
Please let's not start talking about how tables are not made for lay-outing. I'm very aware of the value and shortcomings of CSS and try hard to use it as much as possible.
推荐答案
@Tomalak 评论:
@Tomalak in comments:
因为 ID 选择器前面必须有一个哈希 #,所以这里应该没有歧义
since ID selectors must be preceded by a hash #, there should be no ambiguity here
#id.class"是一个有效的选择器,它需要一个 id 和一个单独的类来匹配;它是有效的,并不总是完全多余的.
"#id.class" is a valid selector that requires both an id and a separate class to match; it's valid and not always totally redundant.
在 CSS 中选择文字."的正确方法是将其转义:#id.moreid".这曾经在一些较旧的浏览器(特别是 IE5.x)中引起问题,但所有现代桌面浏览器都支持它.
The correct way to select a literal ‘.’ in CSS is to escape it: "#id.moreid". This used to cause trouble in some older browsers (in particular IE5.x), but all modern desktop browsers support it.
同样的方法在 jQuery 1.3.2 中似乎确实有效,但我还没有彻底测试它;quickExpr 没有选择它,但更复杂的选择器解析器似乎正确:
The same method does seem to work in jQuery 1.3.2, though I haven't tested it thoroughly; quickExpr doesn't pick it up, but the more involved selector parser seems to get it right:
$('#SearchBag\.CompanyName');
这篇关于当id包含一个点时,如何使用jquery通过ID选择html节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!