当ID包含点时,如何通过jquery的ID选择html节点? [英] How to select html nodes by ID with jquery when the id contains a dot?

查看:122
本文介绍了当ID包含点时,如何通过jquery的ID选择html节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的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屋!

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