博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JQuery选择器转义说明
阅读量:7135 次
发布时间:2019-06-28

本文共 2703 字,大约阅读时间需要 9 分钟。

JQuery选择器

JQuery选择器规则, 借用了css1-3的规则(css选择器规则), 因为css本身也需要一套规则来索引DOM元素, 进而进行样式渲染,例如div.blue 表示目标DOM为 class属性值为blue的div元素。

同时JQuery添加了一些自己的规则, 例如按照查询连接元素 $("[href]")。

这样就衍生出, 一套元字符, 用于表达选择器 合法格式, 故对于 其他部分例如属性值在选择器中的情况, 例如查询href为 www.baidu.com的 链接, 则为 $("[href=‘www.baidu.com’]") 其中.就需要转义, 否则跟div.blue中作为格式的点冲突, 应该写为 $("[href=‘www\\.baidu\\.com’]")。

需要转义的字符, 包括 !"#$%&'()*+,./:;<=>?@[\]^`{|}~

详情见官网描述:

http://api.jquery.com/category/selectors/

Borrowing from CSS 1–3, and then adding its own, jQuery offers a powerful set of tools for matching a set of elements in a document.

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 . Also useful is the blog entry by Mathias Bynens on .

 

CSS 转义

根据上文描述给出的 W3C CSS规范,包括了 关于合法css选择器规则的全部集合。

http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier

其中说明, ID中字符转义效果, 转义的反斜杠总被认为是 ID的一部分:

Note: Backslash escapes are always considered to be part of an or a string (i.e., "\7B" is not punctuation, even though "{" is, and "\32" is allowed at the start of a class name, even though "2" is not).

The identifier "te\st" is exactly the same identifier as "test".

css转义owasp建议:

https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet#RULE_.234_-_CSS_Escape_And_Strictly_Validate_Before_Inserting_Untrusted_Data_into_HTML_Style_Property_Values

Except for alphanumeric characters, escape all characters with ASCII values less than 256 with the \HH escaping format. DO NOT use any escaping shortcuts like \" because the quote character may be matched by the HTML attribute parser which runs first. These escaping shortcuts are also susceptible to "escape-the-escape" attacks where the attacker sends \" and the vulnerable code turns that into \\" which enables the quote.

 

HTML5中的 选择器函数

HTML4没有选择器函数, JQuery提供封装接口, 提供选择器规则, 包含了css定位规则,

技术在革新,HTML5最终提供了js原生接口:

见:

HTML5中类jQuery选择器querySelector的使用 

 

JQuery选择器敏感字符转义函数

jQuery的id选择器不支持特殊字符,需要转义(引入jqSelector()函数)

http://maijunjin.github.io/jquery/2013/10/11/jqueyr-id%E9%80%89%E6%8B%A9%E5%99%A8%E4%B8%8D%E6%94%AF%E6%8C%81%E7%89%B9%E6%AE%8A%E5%AD%97%E7%AC%A6%EF%BC%8C%E9%9C%80%E8%A6%81%E8%BD%AC%E4%B9%89.html

// Escapes special characters and returns a valid jQuery selectorfunction jqSelector(str){    return str.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');}

 

The HTML

The javascript

// ID selector$('#'+jqSelector('id.with,special#characters')).text('My Selected Element');// Class selector$('.'+jqSelector('testElem[1]')).text('My Selected Element');

 

测试代码

                  

 

转载地址:http://dpvrl.baihongyu.com/

你可能感兴趣的文章
iframe高度处理
查看>>
对Largest函数的测试
查看>>
laravel 自定义全局函数
查看>>
How to ssh
查看>>
NOIP 2002 字串变换
查看>>
jQuery选择器之层次选择器
查看>>
<input type="button">和<button>区别
查看>>
MVC模式在Java Web应用程序中的实现
查看>>
自定义值转换器
查看>>
数据库索引
查看>>
Windows Mobile开发资源站点集锦
查看>>
IT兄弟连 JavaWeb教程 Servlet表单数据
查看>>
剑法三套,程序员也能赚大钱(2) 转
查看>>
C# OpenFileDialog用法
查看>>
个人第一款小工具-批量文件重命名By Qt 5(Qt 5.2.1 + MSVC2012)
查看>>
《Java EE 开发技术与案例教程》 这是一本好书啊:简洁精辟(相见恨晚)
查看>>
十、装饰(Decorator)模式 --结构模式(Structural Pattern)
查看>>
WWDC 2013 Session笔记 - UIKit Dynamics入门
查看>>
5月7日——采用第三方页面内容,但是顶部title使用自己的
查看>>
RGBa颜色 css3的Alpha通道支持
查看>>