Home  >  Article  >  Web Front-end  >  Detailed explanation of how to handle special symbols in jQuery selectors

Detailed explanation of how to handle special symbols in jQuery selectors

小云云
小云云Original
2017-12-29 10:10:051747browse

本文主要和大家介绍详解jQuery选择器中的特殊符号如何处理。一般情况下,在jQuery选择器中,我们很少会用到诸如“.”、“#”、“(”、“[”等特殊字符,因为根据W3C规定,HTML文档中属性的值是不能包含有这些个特殊字符的,但是在实际应用中,偶尔也会遇到表达式中含有“#”和“.”等特殊字符,那么是如何处理这些个特殊字符的呢?

HTML代码:


<p id="id.a">aa</p> 
<p id="id#b">bb</p>

Jquery代码:


var $id_a = $(&#39;#id.a&#39;);//jQuery对象,实际上是没取到元素的 
var $id_b = $(&#39;#id#b&#39;);//jQuery对象,实际上是没取到元素的 
alert( $id_a.length);//输出0 
alert( $id_b.length);//输出0 
var $id_right_a = $(&#39;#id\\.a&#39;);//jQuery对象,对特殊字符,我们转义一下 
var $id_right_b = $(&#39;#id\\#b&#39;);//jQuery对象,对特殊字符,我们转义一下 
alert( $id_right_a.html());//正确输出"aa" 
alert( $id_right_b.html());//正确输出"bb"

相关推荐:

Oracle字符串中包含数字、特殊符号的排序问题解决方法

get请求传特殊符号的问题

JavaScript实现输入字符串是否包含特殊符号或表情的方法介绍

The above is the detailed content of Detailed explanation of how to handle special symbols in jQuery selectors. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn