[英]how to select element whose id contains | with jquery id selector
If an element id is "mainid|label"
, it will throw exception when using $("#mainid|label")
. 如果元素id是
"mainid|label"
,则在使用$("#mainid|label")
时会抛出异常。
Error: Syntax error, unrecognized expression: #mainid|label
Then how to get this element with jquery id selector? 那么如何使用jquery id选择器获取此元素?
You can also use $.escapeSelector
您还可以使用
$.escapeSelector
$("#"+$.escapeSelector("mainid|label"))
$.escapeSelector
automatically manage every single special character present in your string $.escapeSelector
自动管理字符串中的每个特殊字符
Since | 自| is a meta character, you have to escape it
是一个元字符,你必须逃脱它
$("#mainid\\|label")
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").
要使用任何元字符(例如!“#$%&'()* +,。/:; <=>?@ [] ^`{|}〜)作为名称的文字部分,必须使用两个反斜杠进行转义:\\。例如,id =“foo.bar”的元素可以使用选择器$(“#foo \\ .bar”)。
Description: Selects elements that have the specified attribute, with any value.
描述:选择具有指定属性的元素,具有任何值。
console.log($('[id="mainid|label"]').text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id='mainid|label'>mainid|label</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.