使用jQuery在ID中选择带有百分号(%)的元素 [英] Select element with percent sign (%) in its id with jQuery

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

问题描述

我有一个像这样的元素:

I have an element like this:

<a id="my%20id" href="#">hello</a>

我一直在拼命尝试用jQuery选择它,但是不能.我尝试过:

I've been trying desperately to select it with jQuery, but cannot. I've tried:

$('a#my id')                          // obviously won't work
$('a#my\ id')                         // no such luck
$('a#my%20id')                        // unrecognized expression
$('a#my\%20id')                       // still unrecognized
$('a#' + encodeURIComponent('my id')) // same thing as 'a#my%20id'

是否可以使用jQuery完全选择它?

Is it possible to select this at all with jQuery?

推荐答案

您可以使用属性选择器,在引号中输入ID即可获得准确的ID.

You can use attribute selector, giving id in quotes will get the exact id.

实时演示

Live Demo

$('[id="my%20id"]')

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

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