带有空格的jquery ID [英] jquery IDs with spaces
问题描述
有没有人知道当ID有空格时如何通过jQuery ID在DOM中选择一个项目?
Does anyone know how to select an item in the DOM by ID with jQuery, when that ID has a space?
例如,我的商品的ID为
For example, the ID of my item would be
<div id="content Module">Stuff</div>
我将如何使用jQuery选择它?
How would I select this with jQuery?
如果我愿意
$("#content Module").whatever()
jQuery将尝试查找同时具有内容ID和模块ID的项目,这不是我想要的.
jQuery will try to find an item with both the ID of content and the ID of Module, which is not what I am looking for.
我应该补充一点,我正在使用一个旧的代码库,其中两个单词ID广泛使用,因此遍历和更改所有ID都是不好的.
I should add that I am working with an old code base where these two word ids are used extensively, so going through and changing all the IDs would be bad.
推荐答案
使用属性选择器.
$("[id='content Module']").whatever();
或者更好的是,也指定标签:
Or, better, specify the tag as well:
$("div[id='content Module']").whatever();
请注意,与$('#id')不同,如果页面中有多个具有相同ID的元素,则它将返回多个元素.
Note that unlike $('#id'), this will return multiple elements if you have multiple elements with the same id within your page.
这篇关于带有空格的jquery ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!