2013-06-05 29 views
-1

我有這個很老的JS,在IE8的工作是在做這樣的:按名稱獲取IFRAME元素不再有效?

window.document.getElementById("frameModalityList") 

這裏是我想取什麼:

<iframe name="frameModalityList" src="iframe_advanced_list.jsp" frameborder="0" marginheight="0" marginwidth="0" class="advanced_list" width="100%" height="300"></iframe> 

現在,當我嘗試在IE9/10,鉻,我得到一個錯誤:

SCRIPT5007: Caught exception occurred : Unable to get property 'contentWindow' of undefined or null reference

什麼是獲得IFRAME元素的新正確方法?

+1

將'name'改爲'id'? –

+0

它曾經工作過,做過這件事的人,在每個地方都會使用'getElementById',這真是一種痛苦......我想當時沒事,我在6年前談論。 –

+0

當在Internet Explorer中使用getElementById時,要小心id和name屬性的混淆......請參閱我的回答中的鏈接 – pinkpanther

回答

2

新編輯:那舊代碼工作,因爲Internet Explorer舊版本中可能存在的一些錯誤(或意圖)。

從這個Beware of id and name attribute mixups when using getElementById in Internet Explorer

當使用的getElementById獲得通過id屬性引用到一個元素,IE瀏覽器的Windows(和Opera的一些版本)也將匹配的元素,其名稱屬性包含相同的值。

首先給出答案:

你被id獲取,但你沒有的idiframe元素

window.document.getElementById("frameModalityList")

變化name屬性ID iframe設置爲

<iframe id="frameModalityList" src="iframe_advanced_list.jsp" frameborder="0" marginheight="0" marginwidth="0" class="advanced_list" width="100%" height="300"></iframe> 
1

因爲你還沒有設置ID,但名字試試這個:

window.document.getElementByName("frameModalityList") 

或插入標識加入HTML:

<iframe id="frameModalityList" name="frameModalityList" src="iframe_advanced_list.jsp" frameborder="0" marginheight="0" marginwidth="0" class="advanced_list" width="100%" height="300"></iframe> 

window.document.getElementById("frameModalityList")