2011-06-21 36 views
5

我在JavaScript中使用了拆分功能。它在Firefox和Chrome中運行正常,但是當我調用split函數時,IE顯示錯誤。有沒有辦法使用其他功能,如分裂?JavaScript拆分功能在IE中不起作用

+5

需要查看你的代碼,你得到的錯誤是什麼? – kinakuta

+4

它支持,它可能會在代碼中的其他內容上失敗。發佈你的代碼,我們會看到。 –

+0

我們會盡快回復您的代碼。粘貼後請選擇您的代碼,然後點擊{} – mplungjan

回答

6

split Method

它是由IE8

split method for JScript 5.6

完全支持它也完全由IE6

Live example使用.split(/\s+/)

經測試,在IE9標準,IE9 IE8模式,IE9支持IE7模式和IE9怪癖模式。所有的工作。

編輯:

原來你的實際問題是使用.textContent。這在IE中不起作用。有兩種選擇。

特徵檢測:

var str; 
if (el.textContent) { 
    str = el.textContent; 
} else { 
    str = el.innerText; 
} 

.nodeValue:

var str = el.nodeValue;

+0

我明白我的問題,我使用該代碼textContent.split(/ \ s + /);所以textcontent在ff和crome中工作,但不在ie中工作。解決方案是使用innertext,這是不工作的FF所以有任何其他解決方案 –

+1

@VbPatel是'.textContent'不支持。使用'.nodeValue' – Raynos

0

有IE之間低於#9和大多數其他瀏覽器與涉及當至少一個差字符串拆分 -

var s ='在某些瀏覽器中,您可以在父親化的分隔符上「拆分」一個字符串,並返回數組中的「拆分」位。

s.split(/(?['"] ?)/).join('\n') 

/***************************************************/ 
Firefox 4.0.1>> 
In some browsers, you can 
" 
split 
" 
a string on a parenthized delimeter and return the 
" 
split-off 
" 
bits in the array. 
/***************************************************/  
MSIE 8.0>> 
In some browsers, you can 
split 
a string on a parenthized delimeter and return the 
split-off 
bits in the array. 
/***************************************************/  
MSIE 9.0>> 
In some browsers, you can 
" 
split 
" 
a string on a parenthized delimeter and return the 
" 
split-off 
" 
bits in the array. 
1

當您拆分數字而不是字符串時,使用Javascript拋出 - Object不支持此屬性。

確保在var中有一個字符串值。