使用jQuery选择带有冒号的ID [英] Selecting an ID with a colon in it with jQuery

查看:112
本文介绍了使用jQuery选择带有冒号的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个网站编写一个预先编写的模块,因此我需要定位一个ID为test:two的元素.现在,此元素中有一个冒号,因此jQuery大概并且可以理解地将"two"视为伪类.有什么办法可以用jQuery定位此元素吗?

I'm working on a pre-written module for a site, and I need to target an element with the id test:two. Now, this element has a colon in it, so jQuery is presumably and understandably seeing the 'two' as a pseudo class. Is there any way of targeting this element with jQuery?

此外,无法更改ID.相信我,如果可以的话.

Also, changing the ID is not possible. Believe me, if I could I would.

我举了一个例子:

$('#test').css('background','red');
$(document.getElementById('test:two')).css('background','blue');
$('#test:two').css('background','green');

<script src="//code.jquery.com/jquery-1.6.3.js"></script>

<div id="test">test</div>
<div id="test:two">test two</div>

推荐答案

只需使用\\逃脱冒号:

$('#test\\:two');

http://jsfiddle.net/zbX8K/3/

请参阅文档:

See the docs: How do I select an element by an ID that has characters used in CSS notation?.

这篇关于使用jQuery选择带有冒号的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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