jQuery CSS选择器选择一个在id名称中带有斜杠的id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5153986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:39:05  来源:igfitidea点击:

CSS selector to select an id with a slash in the id name?

jqueryjquery-selectors

提问by hawkeye

I've got <span id="/about-us">being generated by this CMS I'm using.

<span id="/about-us">正在使用这个 CMS 生成我。

I'd like to select this element with jQuery but it doesn't seem to like selecting elements with a slash in them.

我想用 jQuery 选择这个元素,但它似乎不喜欢选择带有斜线的元素。

Is this possible?

这可能吗?

回答by AGuyCalledGerald

you can do

你可以做

$("#\/about-us")

      

      

回答by Furqan Hameedi

you can do it like this

你可以这样做

     $("span[id*='/about-us']")

where it will return the span with '/about-us' in it's id attribute.

它将在其 id 属性中返回带有“/about-us”的跨度。

回答by Kevin

Use the regular way:

使用常规方式:

document.getElementById('id/with/slashes')

回答by Hemendra Singh

You can use jQuery escapeSeletor to do this.

您可以使用 jQuery escapeSeletor 来做到这一点。

$("#" + $.escapeSelector("id/with/slashes"))