简体   繁体   English

HTML名称属性中的大括号

[英]Curly braces in HTML name attribute

I'm trying to retrieve DOM elements based on their name. 我正在尝试根据名称检索DOM元素。 Unfortunately their names have curly braces {} inside of them. 不幸的是,他们的名字里面有花括号{}

jQuery (1.6.4) is able to retrieve them without a problem. jQuery(1.6.4)能够毫无问题地检索它们。 But at this point I am trying to replace these calls with getElementsByName() , but gEBN is not matching anything that has curly brackets in the query string. 但此时我正在尝试用getElementsByName()替换这些调用,但是gEBN不匹配查询字符串中具有大括号的任何内容。 I have tried backslash escaping it once and twice to no avail. 我试过反斜杠一次又一次地逃避它无济于事。

Is there a way to do this without changing the convention of using the curly brackets? 有没有办法在不改变使用花括号的约定的情况下执行此操作?

It also looks like they are not in the set of characters allowed in the name attr. 它看起来也不在名称attr中允许的字符集中。

Update : Seems to work in Chrome but not Safari for Windows. 更新 :似乎可以在Chrome中使用,但不能在Safari for Windows中使用。 But what's really strange is that even Safari for Windows handles those fiddles fine. 但真正奇怪的是,甚至Safari for Windows也能很好地处理这些小提琴。

If you read the jQuery documentation page on selectors , you'll see this: 如果您阅读选择器上的jQuery文档页面,您会看到:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\\\ . For example, an element with id="foo.bar", can use the selector $("#foo\\\\.bar") . 要使用任何元字符(例如!“#$%&'()* +,。/:; <=>?@ [] ^`{|}〜)作为名称的文字部分,必须使用两个反斜杠进行转义: \\\\ 。例如,id =“foo.bar”的元素可以使用选择器$("#foo\\\\.bar")

You should expect "strange" results when you do things that are explicitly outside what the standard allows. 当您执行明确超出标准允许范围的事情时,您应该期待“奇怪”的结果。 If you choose to go off the reservation, you're kinda on your own. 如果您选择退出预订,那么您自己就是一个人。 Some browsers will work, some won't. 有些浏览器会工作,有些则不会。 Ones that used to work will break without warning when a new version comes out. 过去工作的人会在新版本发布时不会发出警告而中断。

So it depends what you're trying to do. 所以这取决于你想要做什么。 If you're just building something for yourself or a small team, this might be okay. 如果你只是为自己或小团队建立一些东西,这可能没问题。 If you're building something for a general audience, better to stick to the standards and not use dis-allowed characters. 如果你正在为一般观众建立一些东西,最好坚持标准而不是使用不允许的角色。

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

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