javascript - 选择一个 id 包含 '

标签 javascript jquery

我有一个 id 看起来像的元素

link-21-'some-text'''''sometext'-1

我无法更改源代码中的 id。有没有办法使用 id 选择它们?

jQuery("#link-21-'some-text''''''sometext'-1") 出于显而易见的原因抛出错误。有什么解决办法吗?

最佳答案

因为它包含一些特殊含义的字符,所以使用 attribute equals selectorescapes the special meaning character .

检查 j Query selctor docs :

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \. For example, an element with id="foo.bar", can use the selector $("#foo\.bar").The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.


所以它可以像跟随或转义每个元字符。

jQuery('[id="link-21-&apos;some-text&apos;&apos;&apos;&apos;sometext&apos;-1"]')

关于javascript - 选择一个 id 包含 ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782273/

相关文章:

javascript - Google map API - LatLng 返回 NaN

jquery - 延迟动画的开始

javascript - Kendo UI TreeView DragEnd 事件崩溃极其缓慢

javascript - 如何高效使用jquery和javascript?

javascript - 修复生产 react 路由时,fetch 找不到 api url

javascript - ".JPG"和 ".jpg"用于 img src

javascript - 通过查询仅删除一个 div

javascript - JSON.parse node.js 中的意外结果

javascript - 如何有效地计算 JavaScript 中对象的键/属性的数量

jquery - jQuery 可以返回特定子元素为空的所有元素吗?