最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery selector with ids and comma - Stack Overflow

programmeradmin4浏览0评论

I am using MyTableGrid to show an Excel like control in my webpage.

The cells are referenced with ids like "mtgIC1_0,2" for table 1, column 0, row 2.

Unfortunately, when I try to use the jQuery selector with this id $("#mtgIC1_0,2"), it never works.

I figured it is because of the "," since it works for any other ids in the page without coma.

I am using MyTableGrid to show an Excel like control in my webpage.

The cells are referenced with ids like "mtgIC1_0,2" for table 1, column 0, row 2.

Unfortunately, when I try to use the jQuery selector with this id $("#mtgIC1_0,2"), it never works.

I figured it is because of the "," since it works for any other ids in the page without coma.

Share Improve this question edited Jan 27, 2016 at 22:29 user4639281 asked Jul 8, 2010 at 23:46 EricEric 19.9k19 gold badges88 silver badges149 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 14

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")

However if you escape the comma it should still work

e.g

$('#mtgIC1_0\\,2')

From here http://api.jquery.com/category/selectors/

"If you wish to use any of the meta-characters (#;&,.+*~':"!^$[]()=>|/ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an an input with name="names[]", you can use the selector $("input[name=names\\[\\]]")."

I don't know offhand if this is the reason, but according to here, ID names should not contain commas. The relevant sentence is:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

If you have known coordinates of the table, you can target the cell like this:

$('#myTable tr:nth-child(2) td:nth-child(2)').css('background-color', '#F00');
发布评论

评论列表(0)

  1. 暂无评论