JQuery ID选择器中的不能包含特殊字符的处理

开发 前端
最近在开发一个界面时发现了某些特殊情况下ID选择器就会出现无效的情况,查明是ID选择器中的不能包含特殊字符的原因。本文将介绍解决方法。

问题的起因是动态生成的Dom 元素的ID中包含“=”导致(你可能会问为什么会在ID中有“=”号,我只能说这种情况虽然不多,但是有,比如我的情况,我的ID是某个字符串Base64编码之后的字符串)。

JQuery中的1.2.6版本至1.3.2版本都有这种情况,下面是测试的代码:

view plaincopy to clipboardprint?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
    <title></title> 
    <script src="Javascript/jquery.1.3.2.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
        $(function() {              
            var div = $("#hellodiv=");  
            if (div.length > 0) {  
                alert("获取到了Div");  
            }  
            else {  
                alert("哎呀ID中不能包含=");  
            }  
            var div2 = document.getElementById("hellodiv=");  
            if (div2) {  
                alert("我可以获取到哦");  
            }  
            else {  
                alert("哎呀我也获取不到");  
            }  
        });  
    </script> 
</head> 
<body> 
    <div id="hellodiv="></div> 
</body> 
</html> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script src="Javascript/jquery.1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {           
            var div = $("#hellodiv=");
            if (div.length > 0) {
                alert("获取到了Div");
            }
            else {
                alert("哎呀ID中不能包含=");
            }
            var div2 = document.getElementById("hellodiv=");
            if (div2) {
                alert("我可以获取到哦");
            }
            else {
                alert("哎呀我也获取不到");
            }
        });
    </script>
</head>
<body>
    <div id="hellodiv="></div>
</body>
</html>查看Jquery的源代码可以看到堆选择器的解析有这么一段:

view plaincopy to clipboardprint?
var match = quickExpr.exec( selector );  
 
        // Verify a match, and that no context was specified for #id  
        if ( match && (match[1] || !context) ) {  
 
            // HANDLE: $(html) -> $(array)  
            if ( match[1] )  
                selector = jQuery.clean( [ match[1] ], context );  
 
            // HANDLE: $("#id")  
            else {  
                var elem = document.getElementById( match[3] ); 

    var match = quickExpr.exec( selector );

            // Verify a match, and that no context was specified for #id
            if ( match && (match[1] || !context) ) {

                // HANDLE: $(html) -> $(array)
                if ( match[1] )
                    selector = jQuery.clean( [ match[1] ], context );

                // HANDLE: $("#id")
                else {
                    var elem = document.getElementById( match[3] );其中quickExpr是个正则表达式对象

quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,

^#([\w-]+)$是判断ID选择符,很明显只能匹配包括下划线的任何英文字符数字和下划线中划线。

所以其他的字符如= @等都会出现问题。你解决的办法可以修改JQuery代码中的正则表达式

如我要添加=号,那么我可以改成quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-\=]+)$/,

或者避免出现=的ID出现。。随便,本文只是为了大家遇到类似问题时可以快速找到问题。

【编辑推荐】

  1. 使用jQuery和PHP构建一个受Ajax驱动的Web页面
  2. 使用 jQuery 简化 Ajax 开发
  3. 跟ASP.NET MVC一起使用jQuery
责任编辑:彭凡 来源: 博客园
相关推荐

2012-06-12 09:43:11

jQuery

2010-09-07 12:56:49

id选择器CSS

2010-07-20 10:11:32

jQuery选择器Sizzle

2010-12-27 16:01:45

jQuery选择器

2010-09-26 16:51:03

SQL Server查

2013-12-02 14:22:14

jQuery选择器

2010-06-25 09:04:43

jQuery选择器

2010-09-03 09:30:29

CSS选择器

2010-09-06 09:50:34

id选择器CSS

2010-08-26 12:53:40

CSSid选择器

2009-07-16 11:02:33

Swing文件选择器

2013-03-11 10:30:56

CSSWeb

2015-07-13 11:28:22

Linux文件名

2009-11-26 09:52:05

jQuery选择器

2011-04-26 15:07:48

jQuery

2023-01-30 08:42:33

CSS选择器性能

2010-09-07 10:19:31

SQL语句

2012-04-16 14:32:31

iOS选择器代码

2020-10-25 08:57:56

CSS前端浏览器

2018-06-22 15:46:45

Spring Clou加密处理
点赞
收藏

51CTO技术栈公众号