jQuery jQuery.escapeSelector()Methods

jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Examples

Use the .escapeSelector() method to escape a character "#" contained in an ID

<div> <p id='#target'>some text</p> <span id='target'>some text</span> </div> <script> $(function () { //Will select id=#target $( "div" ).find( "#" + $.escapeSelector( "#target" )).css('color','red'); }) </script>

Try it »

Definition and Usage

$.escapeSelector() function is used to escape characters or strings that have special meaning in CSS selectors.


Tip:1. Added in jQuery 3.0, $.escapeSelector() can be used reliably in all browsers supported by jQuery.
2. This method is very useful when a CSS class name or an ID contains a character that has special meaning in CSS, such as a dot or semicolon.


Syntax

$.escapeSelector( selector )

Parameters Description
selector Selector type A string containing a selector expression to be escaped.


实例

More Examples

Select all elements with class name .box inside a div
Select all elements with class name .box inside a div.


jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Other Extensions