如何转义包含 <b> 的 id
How to escape id containing <b>
我正在尝试在 jQuery 中转义这个(德语)id
CSS 选择器(注意 <b>
):
$('[id="fa_form_row_<b>Wer ist der teilnehmer/in (Mehrfachantworten möglich)?</b> des Kurses:"]').toggleClass('inactive');
我该怎么做?
即使你可以这样做,你也不应该。正如您从 MDN docs 中看到的那样,id
属性不能包含这些您已经拥有的一些字符:
This attribute's value must not contain whitespace (spaces, tabs etc.).
并且:
Using characters except ASCII letters and digits, '_', '-' and '.' may cause compatibility problems, as they weren't allowed in HTML 4. Though this restriction has been lifted in HTML 5, an ID should start with a letter for compatibility.
official HTML spec 证实了这一点:
The value must not contain any space characters.
对于HTML4:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
只需使用(双)反斜杠:
$('[id="fa_form_row_\<b\>Wer ist der teilnehmer/in (Mehrfachantworten möglich)?\</b\> des Kurses:"]').toggleClass('inactive');
(来源:CSS Tricks)
另请查看 CSS.escape()
,这是一项 实验性 技术,在 Chrome 和 Firefox 中得到了一些支持:
var selector = CSS.escape('[id="fa_form_row_<b>Wer ist der teilnehmer/in (Mehrfachantworten möglich)?</b> des Kurses:"]');
$(selector).toggleClass('inactive');
我正在尝试在 jQuery 中转义这个(德语)id
CSS 选择器(注意 <b>
):
$('[id="fa_form_row_<b>Wer ist der teilnehmer/in (Mehrfachantworten möglich)?</b> des Kurses:"]').toggleClass('inactive');
我该怎么做?
即使你可以这样做,你也不应该。正如您从 MDN docs 中看到的那样,id
属性不能包含这些您已经拥有的一些字符:
This attribute's value must not contain whitespace (spaces, tabs etc.).
并且:
Using characters except ASCII letters and digits, '_', '-' and '.' may cause compatibility problems, as they weren't allowed in HTML 4. Though this restriction has been lifted in HTML 5, an ID should start with a letter for compatibility.
official HTML spec 证实了这一点:
The value must not contain any space characters.
对于HTML4:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
只需使用(双)反斜杠:
$('[id="fa_form_row_\<b\>Wer ist der teilnehmer/in (Mehrfachantworten möglich)?\</b\> des Kurses:"]').toggleClass('inactive');
(来源:CSS Tricks)
另请查看 CSS.escape()
,这是一项 实验性 技术,在 Chrome 和 Firefox 中得到了一些支持:
var selector = CSS.escape('[id="fa_form_row_<b>Wer ist der teilnehmer/in (Mehrfachantworten möglich)?</b> des Kurses:"]');
$(selector).toggleClass('inactive');