javascript string.search无法使用chrome

时间:2012-04-16 17:51:49

标签: javascript regex google-chrome

目前我对Chrome有一个奇怪的问题......这就是我想要实现的目标:

我有一系列的表格部分,相应地标识了它们的ID,如下所示:

T = Tab  
G = Group within Tab  
S = Sub-Group within Group  
# = Numerical index  

for example:  
<tr id="T1"> = Tab 1  
<td id="T1G3"> = Tab 1 , Group 3  
<td id="T1G3S1"> = Tab 1, Group 3, Sub-Group 1  

到目前为止非常简单...在JavaScript的帮助下,我还打算在表单上启用或禁用这些组。现在,这是我遇到的问题......当我第一次加载表单时,我想禁用表单上的所有内容。为此,我创建了一个动态函数,可以为我做,我将指定哪些标签受影响,以及在这些标签的ID中查找什么,如果匹配发生,请禁用它,如下所示:

Pseudo and Definition:
Function DisableAll(string TagNamesCSArray, string RegExpContent)
{
Split the tag names provided into an array
- loop through the array and get all tags using document.getElementsByTagName() within page
-- if tags are found
--- loop through collection of tags/elements found
---- if the ID of the element is present, and MATCHES the RegExp in any way
----- disable that item
---- end if
--- end loop
-- end if
- end loop
}

这很容易实现,这是最终的结果:

function DisableAll(TagNames, RegExpStr) 
{
//declare local vars
var tagarr = TagNames.split(",");
var collection1;
var IdReg = new RegExp(RegExpStr);
var i;

//loop through getting all the tags
for (i = 0; i < tagarr.length; i++) 
{
    collection1 = document.getElementsByTagName(tagarr[i].toString())
    //loop through the collection of items found, if found
    if (collection1) 
    {
        for (y = 0; y < collection1.length; y++) 
        {
            if (collection1[y].getAttribute("id") != null) 
            {
                if (collection1[y].getAttribute("id").toString().search(IdReg) != -1) 
                {
                    collection1[y].disabled = true;
                }
            }
        }
    }
}
return;

}

然后我这样打电话给它:

DisableAll("tr,td", "^T|^T[0-9]S");

看起来很简单吗? “Hannnn!”错误的答案蝙蝠侠...这在所有浏览器中都能正常工作,除了Chrome ......现在为什么会这样?我不明白。也许我的RegExp出了问题?

非常感谢任何帮助。

干杯!

MaxOvrdrv

1 个答案:

答案 0 :(得分:0)

在我的情况下,正则表达式匹配所有可能性。但是行collection1[y].disabled = true;没有效果,因为禁用不是DOM节点的属性。

顺便说一句:你的正则表达式的第二部分是不必要的,因为&#34; ^ T&#34;将匹配以T开头的每个ID跟随一个数字。