2012-04-16 56 views
0

我有此刻的Chrome存在的問題奇特工作......這就是我要完成的:的JavaScript string.search無法在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

回答

0

對我而言,正則表達式匹配所有可能性。但是行collection1[y].disabled = true;不起作用,因爲禁用不是DOM節點的屬性。

順便說一句:你的正則表達式的第二部分是不必要的,因爲「^ T」將匹配以T開頭的每個ID後跟一個數字。

+0

謝謝...當你迴應了這個,我剛剛得知IE禁用元素不同於其他瀏覽器...例如:我需要禁用匹配的ID內的控件,而不是具有匹配的ID的控件......傷心......因爲這樣做在執行這樣的任務時非常有用......:S – MaxOvrdrv 2012-04-16 18:54:53