开发者

jQuery: how to reference an HTML element whose class name has a period?

开发者 https://www.devze.com 2023-01-19 19:36 出处:网络
I have several elements in this way <td class=\"sede_co.uk\"> I want to reference them using jquery this way:

I have several elements in this way <td class="sede_co.uk">

I want to reference them using jquery this way:

$(td.sede_co.uk)

but jquery doesn't "find" them because (I guess) the point between "co" and "uk".

Any i开发者_如何学运维dea?

Regards

Javi


You should fix the invalid class name...but to query for it as is, escape the .:

$("td.sede_co\\.uk")

You can test it here. Please do fix the class though, . isn't a valid character in a class name...it'll cause fewer headaches.


Oh, come on now, Hannes. I only hunt down people for things like <p class="paragraph">.

If I may, a slight correction. . is a valid character when it's been escaped, just as nearly anything you can type on a keyboard. (It should only require one backslash to escape, though; I'm not sure why JSfiddle thinks it needs two.)

Hogan, you're correct that the point is the reason jQuery isn't finding the elements. That's because $(td.sede_co.uk) is saying "select any td element that has both sede_co and uk in its class attribute's value".

In other words, it would select <td class="sede_co uk"> or <td class="uk sede_co"> or even <td class="sede_co anarchy inthe uk mate">. Note the spaces in those values, which maintain 'separation' between the different words. Since your cell's class value is literally sede_co.uk the selector can't find it. Escape it, and it can.

Or else you could stop using points in class names. It'll probably be easier on you in the long run.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号