我有一个 id 看起来像的元素
link-21-'some-text'''''sometext'-1
。
我无法更改源代码中的 id
。有没有办法使用 id 选择它们?
jQuery("#link-21-'some-text''''''sometext'-1")
出于显而易见的原因抛出错误。有什么解决办法吗?
最佳答案
因为它包含一些特殊含义的字符,所以使用 attribute equals selector或 escapes 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-'some-text''''sometext'-1"]')
关于javascript - 选择一个 id 包含 ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782273/